http://acm.hdu.edu.cn/showproblem.php?pid=1358
寻找循环性前缀,利用KMP周期性。
AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 1000010;
char str[MAX];
int next[MAX];
void getnext(){
int i=0, j=-1,n=strlen(str);
next[0] = -1;
while(i < n){
if(j == -1 || str[i] == str[j]){
i++,j++;
next[i] = j;
}
else j = next[j];
}
}
int main(){
int n,t = 0;
while(scanf("%d",&n) == 1){
if(!n) break;
scanf("%s",str);
getnext();
printf("Test case #%d\n",++t);
for(int i=1; i<=n; i++){
int count = i - next[i];
if(count != i && i % count == 0)
printf("%d %d\n",i,i/count);
}
printf("\n");
}
//system("pause");
return 0;
}