标签:des style blog color os io for art
Problem Description
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],
then the prefix is a “period” of S. We want to all the periodic prefixs.
Input
The first line contains an integer T representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.
Output
1 #include <cstdio> 2 #include <cstring> 3 const int maxn = 1000010; 4 int p[maxn],ans[maxn]; 5 char str[maxn]; 6 7 void get_p(int len){ 8 p[1] = 0; 9 int j = 0; 10 for(int i = 2;i <= len;i++){ 11 while(j > 0 && str[j+1] != str[i]) 12 j = p[j]; 13 if(str[j+1] == str[i]) 14 j++; 15 p[i] = j; 16 } 17 } 18 19 int main(){ 20 int nkase; 21 scanf("%d",&nkase); 22 for(int kase = 1;kase <= nkase;kase++){ 23 scanf("%s",str+1); 24 int len = strlen(str+1); 25 get_p(len); 26 int t = p[len],cnt = 0; 27 while(t){ 28 ans[cnt++] = len-t; 29 t = p[t]; 30 } 31 ans[cnt++] = len; 32 printf("Case #%d: %d\n",kase,cnt); 33 for(int i = 0;i < cnt-1;i++) 34 printf("%d ",ans[i]); 35 printf("%d\n",ans[cnt-1]); 36 } 37 return 0; 38 }
FZU 1901 Period II,布布扣,bubuko.com
标签:des style blog color os io for art
原文地址:http://www.cnblogs.com/zzy9669/p/3871446.html