题目大意:
给出一个字符串 问它最多由多少相同的字串组成
解题思路:
KMP算法的又一个应用
代码如下:
#include<iostream>
using namespace std;
char s[10000001];
int next[10000001];
int L,i,j;
void Index_kmp()
{
while(i<L)
{
if(j==0||s[i]==s[j])
{
++i;j++;next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(1)
{
scanf("%s",s);
if(s[0]=='.')
break;
L=strlen(s);
i=1,j=0;
next[0]=0;
Index_kmp();
if(L%(L-next[L])==0)
printf("%d\n",L/(L-next[L]));
else
printf("1\n");
}
return 0;
}
646

被折叠的 条评论
为什么被折叠?



