KMP算法实现源代码

来源:互联网 发布:网络招聘平台分析报告 编辑:程序博客网 时间:2024/06/05 08:24
  1. #include "StdAfx.h"  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. //代码4-1    
  7. //修正后的求next数组各值的函数代码    
  8. void get_nextval(char const* ptrn, int plen, int* nextval)    
  9. {    
  10.     int i = 0;  //注,此处与下文的代码实现二不同的是,i是从0开始的(代码实现二i从1开始)     
  11.     nextval[i] = -1;    
  12.     int j = -1;    
  13.     while( i < plen-1 )    
  14.     {    
  15.         if( j == -1 || ptrn[i] == ptrn[j] )   //循环的if部分    
  16.         {    
  17.             ++i;    
  18.             ++j;    
  19.             //修正的地方就发生下面这4行    
  20.             if( ptrn[i] != ptrn[j] ) //++i,++j之后,再次判断ptrn[i]与ptrn[j]的关系    
  21.                 nextval[i] = j;      //之前的错误解法就在于整个判断只有这一句。    
  22.             else    
  23.                 nextval[i] = nextval[j];    
  24.         }    
  25.         else                                 //循环的else部分    
  26.             j = nextval[j];    
  27.     }    
  28. }    
  29.   
  30. void print_progress(char const* src, int src_index, char const* pstr, int pstr_index)  
  31. {  
  32.     cout<<src_index<<"\t"<<src<<endl;  
  33.     cout<<pstr_index<<"\t";  
  34.     forint i = 0; i < src_index-pstr_index; ++i )  
  35.         cout<<" ";  
  36.     cout<<pstr<<endl;  
  37.     cout<<endl;  
  38. }  
  39.   
  40. //代码5-1    
  41. //int kmp_seach(char const*, int, char const*, int, int const*, int pos)  KMP模式匹配函数    
  42. //输入:src, slen主串    
  43. //输入:patn, plen模式串    
  44. //输入:nextval KMP算法中的next函数值数组    
  45. int kmp_search(char const* src, int slen, char const* patn, int plen, int const* nextval, int pos)    
  46. {    
  47.     int i = pos;    
  48.     int j = 0;    
  49.     while ( i < slen && j < plen )    
  50.     {    
  51.         if( j == -1 || src[i] == patn[j] )    
  52.         {    
  53.             ++i;    
  54.             ++j;    
  55.         }    
  56.         else    
  57.         {    
  58.             j = nextval[j];              
  59.             //当匹配失败的时候直接用p[j_next]与s[i]比较,    
  60.             //下面阐述怎么求这个值,即匹配失效后下一次匹配的位置    
  61.         }    
  62.     }    
  63.     if( j >= plen )    
  64.         return i-plen;    
  65.     else    
  66.         return -1;    
  67. }    
  68.   
  69. int   main()  
  70. {  
  71.     std::string src = "aabcabcebafabcabceabcaefabcacdabcab";  
  72.     std::string prn = "abac";  
  73.   
  74.     int* nextval = new int[prn.size()];  
  75.     //int* next = new int[prn.size()];  
  76.     get_nextval(prn.data(), prn.size(), nextval);  
  77.     //get_next(prn.data(), prn.size(), next);  
  78.   
  79.     forint i = 0; i < prn.size(); ++i )  
  80.         cout<<nextval[i]<<"\t";  
  81.     cout<<endl;  
  82.       
  83.     cout<<"result sub str: "<<src.substr( kmp_search(src.data(), src.size(), prn.data(), prn.size(), nextval, 0) )<<endl;  
  84.     system("pause");  
  85.   
  86.     delete[] nextval;  
  87.     return 0;  
  88. }  
0 0
原创粉丝点击