HDU 1358 Period(KMP+最小循环节)题解
思路:
这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做
代码:
#include<iostream>
#include<algorithm>
const int N = 1000000+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int fail[N];
char p[N];
void getFail(){
fail[0] = -1;
int j = 0,k = -1;
int len = strlen(p);
while(j <= len){
if(k == -1 || p[j] == p[k]){
fail[++j] = ++k;
}
else{
k = fail[k];
}
}
}
/*int KMP(){
int num = 0;
getFail();
int i = 0,j = 0;
int lent = strlen(t),lenp = strlen(p);
while(i < lent){
if(j == -1 || t[i] == p[j]){
i++;
j++;
if(j == lenp) num++;
}
else{
j = fail[j];
}
}
return num;
}*/
int main(){
int n,Case = 1;
while(scanf("%d",&n) && n){
scanf("%s",p);
printf("Test case #%d\n",Case++);
getFail();
int num = 0;
for(int i = 2;i <= n;i++){
if(fail[i] == -1) continue;
int c = i - fail[i];
if(i == c) continue;
else if(i % c == 0) printf("%d %d\n",i,i / c);
}
printf("\n");
}
return 0;
}