Sample Input
abcd aaaa ababab .
1 4 3
| 下标 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 字符 | a | b | a | b | a | b | a | b | a | b | |
| next | -1 | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int M = 1000010;
int nxt[M];
char T[M];
int n,m;
int ans;
void getnext(){
int j, k;
j = 0; k = -1; nxt[0] = -1;
while(j<m){
if (k==-1 || T[j]==T[k]){
nxt[++j] = ++k;
}
else{
k = nxt[k];
}
}
}
int main()
{
while(scanf("%s",&T)!=EOF){
if(T[0]=='.') break;
m=strlen(T);
getnext();
if(m%(m-nxt[m])==0) printf("%d\n",m/(m-nxt[m]));//****
else printf("1\n");
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/taluoyaxin/article/details/47445439