hdoj1358 //kmp

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;


char a[1000001];
int nextt[1000001];


void kmp(){
int i,j;
int l = strlen(a);
i = 0;j = -1;nextt[0] = -1;
while(i < l){
if(j == -1||a[i] == a[j]){
++i;
++j;
nextt[i] = j;
}
else{
j = nextt[j];
}
}
}


void show(){
for(int i = 2;a[i-1];++i){
int n = i-nextt[i];
if(i%n == 0&&i/n>1){
cout<<i<<" "<<i/n<<endl;
}
}
}


int main(){
int t;
int ok = 0;
while(cin>>t){
if(t == 0)
break;
scanf("%s",a);
kmp();
cout<<"Test case #"<<++ok<<endl;
show();
cout<<endl;
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/dukig/article/details/79680635
今日推荐