• <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年8月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            常用鏈接

            留言簿(2)

            隨筆檔案(7)

            相冊(cè)

            搜索

            •  

            積分與排名

            • 積分 - 15758
            • 排名 - 948

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            #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("耗時(shí):%f毫秒!\n", (clock()-t));
            ???
            ??? t=clock();
            ??? printf("find %d \n", SearchInTxtFile("c:\\txt.txt", "NULL"));
            ??? printf("耗時(shí):%f毫秒!\n", (clock()-t));
            ??? scanf("%c", &ch);

            ??? return 0;
            }?

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

            FeedBack:
            # re: KMP字符串匹配算法C語(yǔ)言實(shí)現(xiàn) 2009-02-18 13:24 楊如清
            錯(cuò)誤的算法
            char str1[] = "abcabcabctasksb,abTo";
            char str2[] = "sb";
            KMP(str1,str2)返回0  回復(fù)  更多評(píng)論
              
            # re: KMP字符串匹配算法C語(yǔ)言實(shí)現(xiàn) 2009-02-27 19:46 gaewah
            的確好像錯(cuò)了厄。。
            abaababaab
            的prefix算錯(cuò)了。。  回復(fù)  更多評(píng)論
              
            # re: KMP字符串匹配算法C語(yǔ)言實(shí)現(xiàn) 2009-08-17 14:45 test
            簡(jiǎn)直就一垃圾 這樣的代碼也貼出來(lái)!  回復(fù)  更多評(píng)論
              
            # re: KMP字符串匹配算法C語(yǔ)言實(shí)現(xiàn) 2011-06-17 10:04 學(xué)校
            樓上有點(diǎn)偏激了。。。  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久国产视屏| 欧美粉嫩小泬久久久久久久 | 欧美日韩中文字幕久久伊人| 久久婷婷人人澡人人爽人人爱| 久久精品国产99国产精品亚洲| 久久夜色tv网站| 99精品久久久久久久婷婷| 91精品国产色综久久| 亚洲av成人无码久久精品 | 亚洲国产另类久久久精品| 亚洲av日韩精品久久久久久a| 久久精品国产一区二区三区日韩| 亚洲精品成人网久久久久久| 亚洲乱码中文字幕久久孕妇黑人 | 国产无套内射久久久国产| 欧洲精品久久久av无码电影| 久久久久免费精品国产| 2019久久久高清456| 亚洲AV无码成人网站久久精品大| 亚洲欧洲精品成人久久奇米网| 久久亚洲高清综合| 久久国产劲爆AV内射—百度| 久久久久亚洲AV无码专区首JN| 久久伊人精品青青草原日本| 亚洲精品美女久久777777| 亚洲色欲久久久综合网| 久久精品国产只有精品2020| 国产高潮国产高潮久久久91| 中文字幕无码av激情不卡久久| 日产精品久久久久久久| AAA级久久久精品无码片| 久久精品中文字幕一区| 人妻精品久久久久中文字幕一冢本| 久久久久亚洲精品无码蜜桃| 无码人妻久久一区二区三区蜜桃| 久久久噜噜噜www成人网| 亚洲国产精品一区二区三区久久 | 狠狠色噜噜色狠狠狠综合久久| 久久精品国产一区| 久久九九有精品国产23百花影院| 中文字幕精品久久久久人妻|