题意:能用最长子串重复得到原串,问重复多少次.
分析:需要考虑这种特殊情况azazaza,正确答案应该是1,所以说,需要判断len%(len-next[len])?=0.别的就是直接输出len/(len-next[len])就行,因为next[len]代表着后缀与前缀多少位是相同的,所以说,最长的肯定是len/(len-next[len]).
wa的原因:1A
AC代码:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <map>
#include <vector>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
char s[1000000];
int next[1000010];
int len;
int main()
{
int i,j;
while(scanf("%s",s)!=EOF&&s[0]!='.')
{
len=strlen(s);
i=0;next[0]=-1;j=-1;
while(i<len)
{
if(j==-1||s[i]==s[j])
{
++i;
++j;
next[i]=j;
}
else j=next[j];
}
if(len%(len-next[len])==0)
{
printf("%d\n",len/(len-next[len]));
}
else printf("1\n");
}
return 0;
}