给定一个字符串,让你求出他最多由几个相同的连续子串连接而成
还是用n - next[n]求出循环节,但是有个比较坑的地方就是一定要判断循环节的长度能否整除总长度
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define maxn 1000009
using namespace std;
int n, Next[maxn];
char s[maxn];
void pre ()
{
Next[0] = Next[1] = 0;
rep (i, 1, n - 1)
{
int j = Next[i];
while (j && s[j] != s[i])
j = Next[j];
Next[i + 1] = s[i] == s[j] ? j + 1 : 0;
}
}
int main ()
{
while (scanf ("%s", s) && s[0] != '.')
{
n = strlen (s);
pre ();
int len = n - Next[n];
if (n % len == 0)
printf ("%d\n", n / len);
else
printf ("1\n");
}
return 0;
}