• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 7  文章 - 15  trackbacks - 0
            <2009年2月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            1234567

            常用鏈接

            留言簿(2)

            隨筆檔案(7)

            相冊

            搜索

            •  

            積分與排名

            • 積分 - 15756
            • 排名 - 948

            最新評論

            閱讀排行榜

            評論排行榜

            #i nclude <stdio.h>
            #i nclude <stdlib.h>
            #i nclude <string.h>
            #i nclude <time.h>

            //獲得prefix數組
            int* GetPrefixValue(char* strPattern, int iPatternLen)
            {
            ??? int i, j; /* i runs through the string, j counts the hits*/
            ??? int* prefix = (int*)malloc(iPatternLen*sizeof(int));
            ???
            ??? i = 1; j = 0;
            ??? prefix[0] = 0;
            ???
            ??? while(i<iPatternLen)
            ??? {
            ??????? if(strPattern[i] == strPattern[j])
            ??????? {
            ??????????? prefix[i] = ++j;
            ??????? }
            ??????? else
            ??????? {
            ??????????? j = 0;
            ??????????? prefix[i] = j;
            ??????? }
            ???????
            ??????? i++;
            ??? }
            ???
            ??? return prefix;??????????
            }???

            //返回target串在pattern串中第一次匹配的index
            int KMPStringMatch(char* strPattern, int iPatternLen, char* strTarget, int iTargetLen, int* prefix)
            {
            ??? int i = 0;
            ??? int j = 0;
            ???
            ??? while(i<iPatternLen && j<iTargetLen)
            ??? {
            ??????? if(j==0 || strPattern[i]==strTarget[j])
            ??????? {
            ??????????? i++;? j++;
            ??????? }
            ??????? else
            ??????? {
            ??????????? j = prefix[j];
            ??????? }
            ??? }????????????
            ???
            ??? free(prefix);
            ????
            ??? if(j==iTargetLen)
            ??? {
            ??????? return i-j;
            ??? }
            ??? else
            ??? {
            ??????? return -1;
            ??? }????????
            }????????

            int KMP(char* strPattern, char* strTarget)
            {
            ??? int* prefix = GetPrefixValue(strPattern, strlen(strPattern));
            ??? int index = KMPStringMatch(strPattern, strlen(strPattern), strTarget, strlen(strTarget), prefix);
            ??? return index;
            }

            //在文本文件中查找target串出現的行數
            int SearchInTxtFile(char* fileName, char* strTarget)
            {
            ??? FILE* hFile = fopen(fileName, "r");
            ???
            ??? char str[1024];
            ??? int count = 0;
            ???
            ???
            ??? while(fgets(str, 1024, hFile))?
            ??? {
            ??????? if(KMP(str, strTarget)!=-1)
            ??????? {
            ??????????? count++;
            ??????? }???
            ??? }?????
            ???
            ??? fclose(hFile);
            ??? hFile=NULL;
            ???
            ??? return count;
            }????
            ????????????
            int main()
            {
            ??? char ch;
            ??? char str1[] = "abcabcabctasksb,abTo";
            ??? char str2[] = "abc";
            ???
            ??? double t=clock();
            ??? printf("%d\n", KMP(str1,str2));
            ??? printf("耗時:%f毫秒!\n", (clock()-t));
            ???
            ??? t=clock();
            ??? printf("find %d \n", SearchInTxtFile("c:\\txt.txt", "NULL"));
            ??? printf("耗時:%f毫秒!\n", (clock()-t));
            ??? scanf("%c", &ch);

            ??? return 0;
            }?

            posted on 2006-07-05 23:48 Bourne 閱讀(4777) 評論(4)  編輯 收藏 引用

            FeedBack:
            # re: KMP字符串匹配算法C語言實現 2009-02-18 13:24 楊如清
            錯誤的算法
            char str1[] = "abcabcabctasksb,abTo";
            char str2[] = "sb";
            KMP(str1,str2)返回0  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現 2009-02-27 19:46 gaewah
            的確好像錯了厄。。
            abaababaab
            的prefix算錯了。。  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現 2009-08-17 14:45 test
            簡直就一垃圾 這樣的代碼也貼出來!  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現 2011-06-17 10:04 學校
            樓上有點偏激了。。。  回復  更多評論
              
            久久久久久国产精品美女 | 国产美女久久精品香蕉69| 国产农村妇女毛片精品久久 | 国内精品久久久久影院免费| 国产午夜福利精品久久2021| 狠狠色丁香婷综合久久| 中文字幕亚洲综合久久2| 亚洲国产精品综合久久一线 | 日韩欧美亚洲综合久久影院d3| 国产伊人久久| 久久精品午夜一区二区福利| 国产精品日韩欧美久久综合| 亚洲精品乱码久久久久久按摩 | 久久青青草原精品影院| 久久亚洲精品国产亚洲老地址 | 久久水蜜桃亚洲av无码精品麻豆 | 伊人久久无码精品中文字幕| 亚洲色欲久久久综合网| 久久se精品一区二区| 久久久久国产精品嫩草影院| 国产成人99久久亚洲综合精品| 久久久久久久波多野结衣高潮| 国产精品欧美久久久久无广告 | 亚洲精品乱码久久久久久蜜桃不卡 | 91亚洲国产成人久久精品| 久久久久久综合网天天| 久久天天躁狠狠躁夜夜不卡| 日本久久久精品中文字幕| 亚洲国产精品无码成人片久久| 欧美精品丝袜久久久中文字幕 | 日韩精品久久无码人妻中文字幕| 国产精品一久久香蕉国产线看 | 影音先锋女人AV鲁色资源网久久| 精品久久久久中文字| 久久亚洲国产欧洲精品一| 日日噜噜夜夜狠狠久久丁香五月| 香蕉久久AⅤ一区二区三区| 久久99精品九九九久久婷婷| 国产成人无码精品久久久免费| 国产亚洲美女精品久久久久狼| 久久免费的精品国产V∧|