有一个字符串s,一次次的重写s,会得到一个新的字符串sssss.....,现在将这个字符串从中切去一部分得到一个字符串t
直接用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) == 1) {
n = strlen (s);
pre ();
int len = n - Next[n];
cout << len << endl;
}
return 0;
}