Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1358

KMP

View Code
 1 #include <cstdio>
 2 using namespace std;
 3 
 4 const int N=1000010;
 5 char s[N];
 6 int fail[N];
 7 void getfail(char *sp)
 8 {
 9     fail[0]=-1;
10     for(int i=1,j=-1;sp[i];i++)
11     {
12         while(j!=-1 && sp[i]!=sp[j+1]) j=fail[j];
13         if(sp[i]==sp[j+1]) j++;
14         fail[i]=j;
15     }
16 }
17 int main()
18 {
19     int n,C=0;
20     while(scanf("%d",&n),n)
21     {
22         printf("Test case #%d\n",++C);
23         scanf("%s",s);
24         getfail(s);
25         for(int i=1;s[i];i++)
26             if(fail[i]!=-1 && (i+1)%(i-fail[i])==0) printf("%d %d\n",i+1,(i+1)/(i-fail[i]));
27         printf("\n");
28     }
29     return 0;
30 }

 

posted on 2012-11-18 19:02  Qiuqiqiu  阅读(99)  评论(0编辑  收藏  举报