Power strings
题目背景:
分析:next数组的妙用,若n % (n - next[n]) == 0 则表示n为循环串,每一串都是长度为n – next[n],证明我的建议是自己下去画一下会更好理解。
Source:
#include
#include
using namespace std;
const int Max = 100000005;
char str[Max];
int len, next[Max];
void get_next(){
int i = 0, j = -1;
next[0] = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
++i; ++j;
next[i] = j;
}
else j = next[j];
}
}
int main(){
while(scanf("%s", str) != EOF){
if(str[0] == '.') break;
len = strlen(str);
get_next();
int ans = 1;
if(len % (len-next[len]) == 0)
ans = len / (len-next[len]);
printf("%d\n", ans);
}
return 0;
}