• <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>

            Problem Solving using C++

            Algorithm Study using C++

            字符串匹配算法(2)---String Matching Algorithm

            前面已經(jīng)說(shuō)到了naive的方法來(lái)匹配字符串,但是該方法的特點(diǎn)是復(fù)雜度高,未能充分利用計(jì)算得到的值作為下一步計(jì)算的結(jié)果,因此,復(fù)雜度相當(dāng)比較高。
            Rabin-Karp提出了新的字符串匹配方法:
            Rabin-Karp方法主要是分2部分:
            (1)預(yù)處理(pre-processing)
            對(duì)pattern字符串的m個(gè)進(jìn)行計(jì)算,得到其hash值。 復(fù)雜度為O(m)
            (2)匹配(matching)
            for(int i=0;i<n-m;i++)
            {
                計(jì)算t[i..i+m]的hash值。
                再同pattern字符串的hash值進(jìn)行對(duì)比,如果相同,則使用naive辦法,繼續(xù)一個(gè)字符一個(gè)字符地比較。
                使用Horner法則計(jì)算下一個(gè)m字符的hash值。(即h(s+1)=f(h(s))).
            }
            整個(gè)算法的復(fù)雜度:
            在最差的情況下,復(fù)雜度為O(m)+O(m*(n-m+1))
            期望的情況為O(n-m+1+cm)=O(n+m).

            實(shí)驗(yàn)代碼如下:(暫時(shí)只是考慮支持12345678901234567890 匹配 234等的情況)
             1 #include <iostream>
             2 #include <string>
             3 
             4 using namespace std;
             5 
             6 #define Q    997 //Should be a prime
             7 #define D    10    //|sizeof character set|
             8 
             9 inline int geth(int m)//h=d^(m-1)%q
            10 {
            11     int len=1;
            12     for(int i=0;i<(m-1);i++)
            13     {
            14         len*=D;
            15     }
            16     return len%Q;
            17 }
            18 
            19 int main(int argc,char* argv[])
            20 {
            21     char *t=NULL,*p=NULL;
            22     string text,pattern;
            23     int h,p0=0,t0=0;
            24 
            25     while(1)
            26     {
            27         int index = 0;
            28 
            29         cin>>text>>pattern;
            30         int n = text.length();        
            31         int m = pattern.length();
            32 
            33         //t= new char[n+1];
            34         //p= new char[m+1];
            35         t= new char[n];
            36         p= new char[m];
            37 
            38         h = geth(m);
            39         p0=t0=0;
            40 
            41         //strcpy(t,text.c_str());
            42         //strcpy(p,pattern.c_str());
            43         memcpy(t,text.data(),n);
            44         memcpy(p,pattern.data(),m);
            45 
            46         for(int i=0;i<m;i++)//get the initial value from pattern and text
            47         {
            48             p0 =(D*p0+(p[i]-'0'))%Q;
            49             t0 = (D*t0+(t[i]-'0'))%Q;
            50         }
            51 
            52         for(int i=0;i<(n-m);i++)
            53         {
            54             if(p0==t0)//should call the naive algorithm to check whether the two string is the same
            55             {
            56                 bool match = true;
            57                 for(int j=0;j<m;j++)
            58                 {
            59                     if(p[j]!=t[i+j])
            60                         match = false;
            61                 }
            62 
            63                 if(match)
            64                     cout<<i<<endl;
            65             }
            66 
            67             t0 = ((D*(t0-(t[i]-'0')*h)+t[i+m])-'0')%Q;
            68         }
            69 
            70         delete[] t;
            71         delete[] p;
            72     }
            73 
            74     system("pause");
            75     return 0;
            76 }
            77 


            posted on 2007-08-20 21:11 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(465) 評(píng)論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品国产精品青草app| 人人妻久久人人澡人人爽人人精品 | 久久精品无码一区二区三区| 久久精品国产亚洲精品2020| 国产精品久久国产精品99盘| 青青草国产成人久久91网| 久久se精品一区二区影院| 久久久久久国产精品无码下载| 欧美亚洲国产精品久久高清| 丰满少妇高潮惨叫久久久| 久久成人18免费网站| 久久久久99精品成人片试看| 久久高清一级毛片| 亚洲av日韩精品久久久久久a| 色噜噜狠狠先锋影音久久| 久久成人小视频| 中文字幕亚洲综合久久2| 色狠狠久久AV五月综合| 欧美日韩中文字幕久久久不卡| 婷婷综合久久中文字幕蜜桃三电影| 亚洲国产精品久久久久网站| 亚洲欧美日韩久久精品第一区| 99久久精品免费| 99久久久国产精品免费无卡顿 | 开心久久婷婷综合中文字幕| 久久精品国产亚洲AV久| 久久有码中文字幕| 久久久精品久久久久久| 久久久久国产精品| 国产日产久久高清欧美一区| 国产精品久久婷婷六月丁香| 狠狠色丁香婷婷综合久久来来去| 欧美午夜精品久久久久免费视| 中文字幕精品无码久久久久久3D日动漫| aaa级精品久久久国产片| 久久99国产综合精品| 97久久精品无码一区二区天美 | 麻豆精品久久久久久久99蜜桃 | 久久亚洲AV成人出白浆无码国产| 久久久久久久女国产乱让韩| 一本久久精品一区二区|