KMP应用举例

1、题目:给定一个字符串,问最多是多少个相同子串不重叠连接构成。


KMP的next数组应用。这里主要是如何判断是否有这样的子串,和子串的个数。

若为abababa,显然除其本身外,没有子串满足条件。而分析其next数组,next[7] = 5,next[5] = 3,next[3] = 1,即str[2..7]可由ba子串连接构成,那怎么否定这样的情况呢?

很简单,若该子串满足条件,则len%sublen必为0。sunlen可由上面的分析得到为len-next[len]。因为子串是首尾相接,len/sublen即为substr的个数。

若L%(L-next[L])==0,n = L/(L-next[L]),else n = 1

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. using namespace std;  
  4.   
  5. char pattern[1000002];  
  6. int next[1000002];  
  7.   
  8. /* 
  9. kmp算法,需要首先求出模式串的next函数值 
  10. next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度 
  11. */  
  12. void get_nextval(const char* pattern)  
  13. {  
  14.     int i=0,j=-1;  
  15.     next[0]= -1;  
  16.     while(pattern[i] != '\0')  
  17.     {  
  18.         if(j== -1 || pattern[i]== pattern[j] )     //pattern[i]表示后缀的单个字符,pattern[j]表示前缀的单个字符  
  19.         {  
  20.             ++i;  
  21.             ++j;  
  22.             if(pattern[i] != pattern[j])  
  23.                 next[i]=j;  
  24.             else  
  25.                 next[i]=next[j];  
  26.         }  
  27.         else  
  28.             j=next[j];    //若j值不相同,则j值回溯  
  29.     }  
  30. }//get_nextval  
  31.   
  32. int main(void)  
  33. {  
  34.     int len;  
  35.     while(scanf("%s",pattern)!=EOF)  
  36.     {  
  37.         if(pattern[0]=='.')  
  38.             break;  
  39.         len=strlen(pattern);  
  40.   
  41.         get_nextval(pattern);             
  42.           
  43.         if(len%(len-next[len])==0)  
  44.             printf("%d\n",len/(len-next[len]));  
  45.         else  
  46.             printf("1\n");  
  47.               
  48.     }  
  49.     return 0;  
  50. }  


2、题目:定义字符串A,若A最多由n个相同字串s连接而成,则A=s^n,

比如"aaa" = "a"^3,"abab" = "ab"^2, "ababa" = "ababa"^1。


分析:KMP。若某一长度L的前缀符合上诉条件,则

1) next[L]!=0(next[L]=0时字串为原串,不符合条件)

2) L%(L-next[L])==0(此时字串的长度为L/next[L])

综上,若L%S==0,则可得L为str[0]...str[s-1]的相同字串组成,总长度为L,其中字串长度SL = s-0+1=L-next[L],循环次数为L/SL。故对于所有大于1的前缀,只要其符合上述条件,即为答案之一。

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. using namespace std;  
  5.   
  6. char pattern[1000002];  
  7. int next[1000002];  
  8.   
  9. /* 
  10. kmp算法,需要首先求出模式串的next函数值 
  11. next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度 
  12. */  
  13. void get_nextval(const char* pattern)  
  14. {  
  15.     int i=0,j=-1;  
  16.     next[0]= -1;  
  17.     while(pattern[i] != '\0')  
  18.     {  
  19.         if(j== -1 || pattern[i]== pattern[j] )  
  20.         {  
  21.             ++i;  
  22.             ++j;  
  23.             next[i]=j;  
  24.         }  
  25.         else  
  26.             j=next[j];  
  27.     }  
  28. }//get_nextval  
  29.   
  30. int main(void)  
  31. {  
  32.     int i,len,n,j=1;  
  33.     while(scanf("%d",&n)!=EOF)  
  34.     {  
  35.         if(!n)  
  36.             break;  
  37.         scanf("%s",pattern);  
  38.         len=strlen(pattern);  
  39.   
  40.         get_nextval(pattern);  
  41.       
  42.         printf("Test case #%d\n",j++);  
  43.         for(i=2;i<=len;i++)  
  44.         {  
  45.             if(i%(i-next[i])==0 && i/(i-next[i])>1)  
  46.                 printf("%d %d\n",i,i/(i-next[i]));  
  47.         }  
  48.         printf("\n");  
  49.               
  50.     }  
  51.     return 0;  
  52. }  


3、题目:给出一个字符串A,求A有多少个前缀同时也是后缀,从小到大输出这些前缀的长度。


分析:对于长度为len的字符串,由next的定义知:
A[0]A[1]...A[next[len]-1]=A[len-next[len]]...A[len-1]此时A[0]A[1]...A[next[len]-1]为一个符合条件的前缀。
有A[0]A[1]....A[next[next[len]]-1] = A[len-next[next[len] - next[next[len]]]...A[next[len]-1],故A[0]A[1]....A[next[next[len]]-1]也是一个符合条件的前缀。
故从len=>next[len]=>next[next[len]] ....=>直到某个next[]为0均为合法答案,注意当首位单词相同时,也为答案。

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<vector>  
  5. using namespace std;  
  6.   
  7. char pattern[400002];  
  8. int next[400002];  
  9.   
  10. /* 
  11. kmp算法,需要首先求出模式串的next函数值 
  12. next[j] = k,说明 p0pk-1 == pj-kpj-1,也就是说k为其前面相等串的长度 
  13. */  
  14. void get_nextval(const char* pattern)  
  15. {  
  16.     int i=0,j=-1;  
  17.     next[0]= -1;  
  18.     while(pattern[i] != '\0')  
  19.     {  
  20.         if(j== -1 || pattern[i]== pattern[j] )  
  21.         {  
  22.             ++i;  
  23.             ++j;  
  24.             next[i]=j;  
  25.         }  
  26.         else  
  27.             j=next[j];  
  28.     }  
  29. }//get_nextval  
  30.   
  31. int main(void)  
  32. {  
  33.     int i,len,n;  
  34.     vector<int>ans;  
  35.     while(scanf("%s",pattern)!=EOF)  
  36.     {  
  37.         ans.clear();  
  38.         len=strlen(pattern);  
  39.         get_nextval(pattern);  
  40.         n=len;  
  41.         while(n)  
  42.         {  
  43.             ans.push_back(n);  
  44.             n=next[n];  
  45.         }  
  46.         if(pattern[0]==pattern[n-1])   //首部、尾部字符相同  
  47.             ans.push_back(1);  
  48.   
  49.         i=ans.size()-1;  
  50.         for(;i>0;i--)  
  51.             printf("%d ",ans[i]);  
  52.         printf("%d\n",ans[0]);  
  53.     }  
  54.     return 0;  
  55. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值