标签:
http://acm.hdu.edu.cn/showproblem.php?pid=3746
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4498 Accepted Submission(s): 2051
#include<stdio.h> #include<string.h> #define N 1000007 char S[N]; int Next[N]; void FindNext(int Slen) { int i=0, j=-1; Next[0] = -1; while(i<Slen) { if(j==-1 || S[i]==S[j]) Next[++i] = ++j; else j = Next[j]; } } int main() { int t; scanf("%d", &t); while(t--) { scanf("%s", S); int i, Slen = strlen(S), w; FindNext(Slen); w = Slen - Next[Slen];///w为循环节的长度 if(w==Slen) ///如果Next[Slen]为0,也就是前Slen位没有循环节,则输出字符串的长度 printf("%d\n", Slen); else if(Slen%w) printf("%d\n", w-Slen%w); else printf("0\n"); } return 0; }
(KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746
标签:
原文地址:http://www.cnblogs.com/YY56/p/4834330.html