zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
    POJ1050 To the Max 最大子矩阵
    POJ1259 The Picnic 最大空凸包问题 DP
    POJ 3734 Blocks 矩阵递推
    POJ2686 Traveling by Stagecoach 状态压缩DP
    iOS上架ipa上传问题那些事
    深入浅出iOS事件机制
    iOS如何跳到系统设置里的各种设置界面
    坑爹的私有API
    业务层网络请求封装
  • 原文地址:https://www.cnblogs.com/weir007/p/6068781.html
Copyright © 2011-2022 走看看