• KMP算法C代码


    贴上C代码作参考,关于算法,可以参考网上的博文,但不要参考太多,一两篇相近的即可。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* 获取pattern的next数组 */
    void get_next(char * pattern, int * next, int len)
    {
        int i, index;
        for (next[0]=-1, i=1; i<len; i++)
        {
            if (pattern[i] == pattern[next[i-1]+1])
            {
                next[i] = next[i-1] + 1;
                continue;
            }
            for (index=next[i-1], next[i]=-1; index!=-1; index = next[index])
            {
                if (pattern[index+1] == pattern[i])
                {
                    next[i] = index + 1;
                    break;
                }
            }
        }
    }
    
    int KMP(char * text, int tlen, char * pattern, int plen, int * ptr)
    {
        int i=0, tindex=0, pindex=0, count=0,  * next = malloc(sizeof(int) * plen);
        get_next(pattern, next, plen);
        while (i < tlen)
        {
            if (pattern[pindex] == text[i])
            {
                i ++;
                pindex ++;
                if (pindex == plen)
                {
                    pindex = 0;
                    ptr[tindex++] = i-plen;
                }
                continue;
            }
            if (pindex == 0)
            {
                    i ++;
            }
            else
            {
                pindex = next[pindex-1] + 1;
            }
        }
        free(next);
        return tindex;
    }
    
    /* 按字符读入文件 */
    int readText(const char * path, char * buf)
    {
        FILE * f;
        int size;
        f = fopen(path, "r");
        if (!f)
        {
            return -1;
        }
        fseek(f, 0, SEEK_END);
        size = ftell(f);
        rewind(f);
        size = fread(buf, 1, size, f);
        if (size <= 0)
        {
            size = -1;
        }
        fclose(f);
        return size;
    }
    
    int main()
    {
        char text[2000] = {0}, pattern[30] = "aba";//"abcabdecabcabcd";
        int * ptr, count = 0, plen, size = readText("file/text.txt", text), i=0, j;
        
        if (size <= 0)
        {
            return -1;
        }
        plen = strlen(pattern);
        ptr = malloc((size/plen)*sizeof(int));
        count = KMP(text, size, pattern, plen, ptr);
        printf("%d results.
    ", count);
        while (i++ < count)
        {
            printf("index[%d] : ", i);
            for (j=0; j<plen; j++)
                printf("%c",text[ptr[i-1]+j]);
            printf("
    ======================
    ");
        }
        free(ptr);
        return 0;
    }
  • 相关阅读:
    centos 6.5 下安装RabbitMQ-3.7.28 二进制版本
    Centos 6.5 Rabbitmq 安装和集群,镜像部署
    Vim 自动添加脚本头部信息
    vim 手动添加脚本头部信息
    Pandas系列教程(11)Pandas的索引index
    Pandas系列教程(10)Pandas的axis参数
    Pandas系列教程(9)Pandas字符串处理
    Pandas系列教程(8)pandas数据排序
    Pandas系列教程(7)Pandas的SettingWithCopyWarning
    Pandas系列教程(6)Pandas缺失值处理
  • 原文地址:https://www.cnblogs.com/weir007/p/6068781.html
Copyright © 2020-2023  润新知