http://poj.org/problem?id=2406
求一个字符串最多由多少个子串重复得到,用kmp里求next数组的方法求得最后一个字母的next【】
如果 next[n]指向的位置的字符与自身相等,且 len%(n-next[n])==0 ,则可证明 字符串存在 len/(n-next[n])个循环节,不满足任一条件,循环节长度只能为1
证明见 :http://blog.csdn.net/viphong/article/details/48498595
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#define inf 0x7fffffff
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 1000005;
using namespace std;
int nextval[maxn];
void get_next(char *t,int len) //失配函数
{
int i,j;
i=1;
nextval[1]=0;
j=0;
while(i<len)
{
if (j==0||t[i]==t[j])
{
j++;
i++;
// if (t[i]!=t[j])
nextval[i]=j;
// else
// nextval[i]=nextval[j];
}
else
j=nextval[j];
}
}
char tm[maxn]; //主串
char nm[maxn];
int judge(int x)
{
return 0;
}
int main( )
{
while(scanf("%s",tm+1)!=EOF)
{
if (strcmp(tm+1,".")==0) break;
int len=strlen(tm+1);
get_next(tm,len);
int maxx=nextval[len];
int ans;
if (len%(len-maxx)==0&& tm[len]==tm[nextval[len]])
ans=len/(len-maxx);
else
ans=1;
printf("%d\n",ans);
}
return 0;
}