POJ-1358 Period
题目大意:POJ-1358
题目大意:问从字符串的开头开始,有多长相同的子串拼接成的子串
解题思路:就是利用kmp的next数组 没找到规律不要紧 把next打出来就清楚了 和POJ-2406 Power Strings差不多
代码块:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int nex[1000005] = {0};
int len;
string strA;
void getNext(){
nex[0] = 0;
int i = 1, j = 0;
while(i < len){
if(strA[i] == strA[j]){
nex[i++] = ++j;
}else if(j != 0){
j = nex[j - 1];
}else{
nex[i++] = 0;
}
}
}
int main(){
int num = 1;
while(cin>>len){
if(!len) return 0;
getchar();
getline(cin, strA);
getNext();
cout<<"Test case #"<<num<<endl;
num++;
for(int i = 0; i < len + 1; i++){
if(nex[i]){
if((i + 1) % (i + 1 - nex[i]) == 0){
cout<<i+1<<" "<<(i + 1) / (i + 1 - nex[i])<<endl;
}
}
}
cout<<endl;
memset(nex, 0, sizeof(nex));
}
return 0;
}