标签:
题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输出k即周期数;
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N = 1e6 + 7; char p[N]; int f[N]; void getfail(char *p,int *f) { f[0] = f[1] = 0; int n = strlen(p); for(int i = 1;i < n;i++){ int j = f[i]; if(j && p[i] != p[j]) j = f[j]; f[i+1] = (p[i] == p[j] ?j+1:0);// i+1会递推到第n位 } } int main() { int n,kase = 1; while(scanf("%d",&n) == 1 && n){ scanf("%s", p); getfail(p,f); printf("Test case #%d\n",kase++); for(int i = 2;i <= n;i++){// i = n ** if(f[i] > 0 && i%(i-f[i])==0) printf("%d %d\n",i,i/(i-f[i])); } puts(""); } }
标签:
原文地址:http://www.cnblogs.com/hxer/p/5268201.html