• <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
            <2011年6月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(2)

            隨筆檔案(7)

            相冊

            搜索

            •  

            積分與排名

            • 積分 - 15785
            • 排名 - 953

            最新評論

            閱讀排行榜

            評論排行榜

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

            //獲得prefix數(shù)組
            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串出現(xiàn)的行數(shù)
            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 閱讀(4782) 評論(4)  編輯 收藏 引用

            FeedBack:
            # re: KMP字符串匹配算法C語言實現(xiàn) 2009-02-18 13:24 楊如清
            錯誤的算法
            char str1[] = "abcabcabctasksb,abTo";
            char str2[] = "sb";
            KMP(str1,str2)返回0  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現(xiàn) 2009-02-27 19:46 gaewah
            的確好像錯了厄。。
            abaababaab
            的prefix算錯了。。  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現(xiàn) 2009-08-17 14:45 test
            簡直就一垃圾 這樣的代碼也貼出來!  回復  更多評論
              
            # re: KMP字符串匹配算法C語言實現(xiàn) 2011-06-17 10:04 學校
            樓上有點偏激了。。。  回復  更多評論
              
            欧美丰满熟妇BBB久久久| 久久无码av三级| 思思久久好好热精品国产| 伊人久久大香线蕉成人| 久久夜色精品国产噜噜噜亚洲AV| 久久午夜羞羞影院免费观看| 久久狠狠色狠狠色综合| 亚洲综合久久久| 久久亚洲欧美日本精品| 色偷偷88欧美精品久久久| 亚洲AV日韩精品久久久久| 国产日韩久久久精品影院首页| 久久精品国产亚洲αv忘忧草| 国产精品一久久香蕉产线看| 久久综合精品国产一区二区三区| 国产99久久精品一区二区| 一本久道久久综合狠狠躁AV| 久久国产精品-久久精品| 久久人人爽人人爽人人片AV不 | 日韩久久无码免费毛片软件| 人妻丰满AV无码久久不卡 | 99久久免费国产精品| 亚洲va久久久噜噜噜久久| 亚洲国产精品综合久久网络| 秋霞久久国产精品电影院| 人妻精品久久久久中文字幕69 | 久久艹国产| 99久久精品国产一区二区蜜芽 | 亚洲伊人久久成综合人影院| 99久久婷婷国产一区二区| 国内精品久久久久久久97牛牛| 欧美激情精品久久久久久| 思思久久99热免费精品6| 久久免费视频6| 亚洲色欲久久久久综合网| 久久精品综合一区二区三区| 国产亚州精品女人久久久久久 | 18岁日韩内射颜射午夜久久成人| 久久亚洲电影| 久久综合九色综合久99| 午夜肉伦伦影院久久精品免费看国产一区二区三区 |