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

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

            作者: kingoal

            給定一個輸入的字符串t(text),長度為n(n=strlen(t)),給出匹配模式p(pattern),長度為m(m=strlen(p)).
            在Introduction to algorithm書(CLRS)中,字符串算法主要是討論了4種,分別對應(yīng)的是:

            樸素(Naive) ----------O(m*(n-m+1))
            Rabin-Karp-----------O(m*(n-m+1))
            有限自動機-----------O(n)
            Knuth-Morris-Pratt------------------O(n)

            下面分4部分具體介紹該4種字符串匹配算法。
            Naive算法非常簡單,就是將t的字節(jié)從0到n-m+1來一一匹配p的m個字符,若所有的m字符都匹配成功,則輸出匹配成功,輸出當前的index值。

            下面給出Naive的實現(xiàn)代碼:

             1 #include <iostream>
             2 #include <string>
             3 
             4 using namespace std;
             5 
             6 int main(int argc,char* argv[])
             7 {
             8     char *t=NULL,*p=NULL;
             9     string text,pattern;
            10 
            11     while(1)
            12     {
            13         int index = 0;
            14 
            15         cin>>text>>pattern;
            16         int n = text.length();        
            17         int m = pattern.length();
            18 
            19         //t= new char[n+1];
            20         //p= new char[m+1];
            21         t= new char[n];
            22         p= new char[m];
            23 
            24         //strcpy(t,text.c_str());
            25         //strcpy(p,pattern.c_str());
            26         memcpy(t,text.data(),n);
            27         memcpy(p,pattern.data(),m);
            28 
            29         for(int i=0;i<n-m+1;i++)
            30         {
            31             bool match=true;
            32 
            33             for(int j=0;j<m;j++)
            34             {
            35                 if(t[i+j]!=p[j])
            36                     match=false;    
            37             }
            38             if(match)
            39                 cout<<index<<endl;
            40 
            41             index++;
            42         }
            43 
            44         delete[] t;
            45         delete[] p;
            46     }
            47 
            48     system("pause");
            49     return 0;
            50 }
            51 

            posted on 2007-08-20 17:22 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(330) 評論(0)  編輯 收藏 引用


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


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            中文字幕日本人妻久久久免费 | 2022年国产精品久久久久| 久久久久久久久久久精品尤物 | 亚洲国产视频久久| 欧美噜噜久久久XXX| 99久久精品毛片免费播放| 久久精品成人免费国产片小草| 亚洲精品高清一二区久久| 99久久人妻无码精品系列蜜桃| 久久亚洲国产欧洲精品一| 久久这里只精品99re66| 欧美日韩中文字幕久久伊人| 亚洲国产精品综合久久网络| 91精品国产高清久久久久久io| 无码人妻久久一区二区三区蜜桃| 久久精品国产亚洲av麻豆小说 | 久久亚洲国产成人精品性色| 蜜桃麻豆www久久| 久久99精品久久久久久久久久| 久久这里只有精品视频99| 久久这里只有精品首页| 无码国产69精品久久久久网站| 亚洲伊人久久成综合人影院| 国产农村妇女毛片精品久久| 久久免费的精品国产V∧| 久久久久亚洲AV无码观看| 狠狠人妻久久久久久综合| 人人狠狠综合久久亚洲88| 国产亚洲精品美女久久久| 亚洲精品美女久久777777| 中文成人久久久久影院免费观看 | 99久久www免费人成精品| 久久―日本道色综合久久| 国产精品久久久久天天影视| 国产成人久久AV免费| 国内精品久久久久影院优| 久久精品国产亚洲av麻豆色欲 | 99久久精品国产一区二区| 99精品久久久久久久婷婷| 精品久久久久久无码中文字幕| 国产免费久久精品99久久|