KMP算法

来源:互联网 发布:新广行风热线网络直播 编辑:程序博客网 时间:2024/06/05 05:02
分类: 算法 3404人阅读 评论(2) 收藏 举报
算法c

题目:

经典的KMP算法

分析:

和KMP算法对应的是BF算法,其中BF算法时间复杂度,最坏情况下可以达到O(n*m),而KMP算法的时间复杂度是O(n + m),所以,KMP算法效率高很多。

但是KMP算法不太好理解,其中牵涉到next数组,目标就是让模式串尽可能的往右滑动,减少比较次数,比如

a  b  a  b  c

-1 0  0  1  2

比如我们比较ababc时,如果c比较发现错误,前面的abab已经比较成功,那么下次比较,我们只需要从aba的最后一个a开始比较,这样省去了从头开始比较。

算法代码:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <cstdio>  
  4. using namespace std;  
  5.   
  6. //这是整个kmp中最核心的地方  
  7. int get_next(const char*t, int *next)  
  8. {  
  9.     int i = 0;  
  10.     int j = -1; //设置j = -1,非常巧妙  
  11.     int len = strlen(t);  
  12.     memset(next,0, sizeof(int) * len);  
  13.     next[0] = -1;  
  14.     while(i < len - 1)  
  15.     {  
  16.         if(j == -1 || t[i] == t[j]) //前面的判断,j == -1, 非常巧妙  
  17.         {  
  18.             i++;  
  19.             j++;  
  20.             next[i] = j;    //将后面的next数组元素赋值  
  21.         }  
  22.         else  
  23.             j = next[j];  
  24.     }  
  25. }  
  26.   
  27. int kmp(const char *s, const char *t)  
  28. {  
  29.     int i = 0;  
  30.     int j = 0;  
  31.     int next[100];  
  32.     get_next(t,next);  
  33.     while(i < strlen(s) && j < strlen(t))  
  34.     {  
  35.         if(j == - 1 || s[i] == t[j])    //如果j为-1,或者模式串和主串相等,两者继续往下比较  
  36.         {  
  37.             i++;  
  38.             j++;  
  39.         }  
  40.         else  
  41.             j = next[j];  
  42.     }  
  43.   
  44.     if(j >= (int)strlen(t))  
  45.     {  
  46.         cout << "found " << endl;  
  47.         return 0;  
  48.     }  
  49.   
  50.     cout << "not found" <<endl;  
  51.     return 0;  
  52. }  
  53.   
  54. //暴力法  
  55. int brute_force(const char *s, const char *t)  
  56. {  
  57.     int i, j;  
  58.     i = 0;  
  59.   
  60.     while(i < strlen(s))  
  61.     {  
  62.         j = 0;  
  63.         while(j < strlen(t))  
  64.         {  
  65.             if(s[i] == t[j])  
  66.             {  
  67.                 i++;  
  68.                 j++;  
  69.             }  
  70.             else  
  71.             {  
  72.                 i = i - j + 1;  
  73.                 break;  
  74.             }  
  75.         }  
  76.   
  77.         if(j == (int)strlen(t))  
  78.         {  
  79.             cout << "found" << endl;  
  80.             return 0;  
  81.         }  
  82.     }  
  83.   
  84.     cout << "not found" << endl;  
  85.     return 0;  
  86.   
  87. }  
  88.   
  89. int main()  
  90. {  
  91.     brute_force("abcdef""abcdef");  
  92.     kmp("abcdef""aaaa");  
  93.     return 0;  
  94.   
  95. }  
0 0
原创粉丝点击