高效面試之字串匹配(KMP,AC演算法)__演算法

來源:互聯網
上載者:User
文本Tn  模式Pm, P在T中出現的位置為位移 字串匹配問題描述為:找出所有位移s(0=<s<=n-m),使得P為Ts+m的尾碼。 分兩步完成,預先處理+匹配
演算法 預先處理時間 匹配時間
樸素演算法 o O((n-m+1)m)
RK演算法 O(m) O((n-m+1)m)
有限狀態機器 O(m|∑|) O(n)
KMP O(m) O(n)
1.樸素字串模式 for s=0 to n-m     ifP[1...m]==T[s+1...s+m]     print "找到位移S"

2.KMP 痛點:求帶匹配的串的首碼next數組
http://blog.csdn.net/yearn520/article/details/6729426
http://www.cnblogs.com/c-cloud/p/3224788.html
http://blog.csdn.net/youxin2012/article/details/17083261
特點:

它可以在匹配過程中失配的情況下,有效地多往後面跳幾個字元,避免不必要的回溯,加快匹配速度。

1.next數組

next數組是用來說明待匹配的串的對稱性,最大公用前尾碼 

         a b c d a b d

next: 0 0 0 0 1 2 0 字串a的最大公用前尾碼為0,a b c d a 最大公用前尾碼為ab,長度為2 最佳化版: void get_next(char str[], int n,int next[])   {       int i = 0;       next[0] = 0;       for(i = 1; i < n; i++)       {           if(str[i] == str[next[i-1]])               next[i] = next[i-1] + 1;           else               next[i] = 0;       }   }   解釋: next[4]表示前長度為4的字串的最大公用前尾碼. 此時如果str[next[4]]與str[5]相等,就可知道next[5]=next[4]+1。 a b c d  a b
next[4]=1 就是b
當不匹配時,將搜尋字詞向後移動(已匹配的字元數 - 對應的next值),設法利用這個已知資訊,不要把"搜尋位置"移回已經比較過的位置,繼續把它向後移,這樣就提高了效率。

已知空格與D不匹配時,前面六個字元"ABCDAB"是匹配的。查表可知,最後一個匹配字元B對應的"部分匹配值"為2,也就是前面2個位元組是不用再去匹配的。下一次匹配:匹配過的AB就不用再匹配了


kmp演算法: src_len源串 dst_len 待匹配的串 while( (i<src_len)&&(j<dst_len) )   {           if(src_string[i] == dst_string[j])           {               i++;               j++;           }           else           {               if(j == 0)               {                   i++; //源字串下表前移動               }               else               {                   m = j - dst_next[j-1];//需回溯的位元                   j = j - m;//設定下一次的起始座標                  }           }      }  
3.AC 多模匹配演算法  看下面這個例子:給定 5 個單詞: say she shr he her ,然後給定一個字串 yasherhs 。 問一共有多少單詞在這個字串中出現過。
三步:構建 trik樹,給trik樹添加 失敗路徑,建立AC自動機,根據 AC自動機 搜尋文本
1.構建trik樹

 1 const int kind = 26; 
 2 struct node{  
 3     node *fail;       //失敗指標
 4     node *next[kind]; //Tire每個節點的個子節點(最多個字母)
 5     int count;        //是否為該單詞的最後一個節點
 6     node(){           //建構函式初始化
 7         fail=NULL; 
 8         count=0; 
 9         memset(next,NULL,sizeof(next)); 
10     } 
11 }*q[500001];          //隊列,方便用於bfs構造失敗指標
12 char keyword[51];     //輸入的單詞
13 char str[1000001];    //模式串
14 int head,tail;        //隊列的頭尾指標
 1   void   buildingTree ( char   * str,node  * root){ 
 2       node  * p = root; 
 3        int  i = 0 ,index;  
 4        while (str[i]){ 
 5           index = str[i] - ' a ' ; 
 6            if (p -> next[index] == NULL) p -> next[index] = new  node();  
 7           p = p -> next[index];
 8           i ++ ;
 9       } 
10       p -> count ++ ;      // 在單詞的最後一個節點count+1,

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.