hdu 2594Simpsons’ Hidden Talents

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

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 char str1[100010];
 4 char str2[50005];
 5 int next[1000010];
 6 int max(int x,int y)
 7 {
 8     return x=x>y?x:y;
 9 }
10 void get_next(int x)
11 {
12     
13     int i=0,j=-1;
14     next[0]=-1;
15     while(i<x)
16     {
17         if(j==-1||str1[i]==str1[j])
18         {
19             i++;
20             j++;
21             //if(str1[i]!=str1[j])
22             next[i]=j;
23             //else next[i]=next[j];
24         }
25         else j=next[j];
26     }
27     
28 }
29 int main()
30 {
31     int len1,len2,len,ans;
32     while(~scanf("%s%s",str1,str2))
33     {
34         len1=strlen(str1);
35         len2=strlen(str2);
36         strcat(str1,str2);
37         len=strlen(str1);
38         get_next(len);
39         ans=next[len];
40         //printf("%d]",ans);
41         while(ans>len1)
42         {
43             ans=next[ans];
44             
45         }
46         while(ans>len2)
47         {
48             ans=next[ans];
49             
50         }
51         for(int i=0;i<ans;i++)
52         printf("%c",str1[i]);
53         if(ans!=0) printf(" ");
54         printf("%d\n",ans);
55         
56     }
57 }

 

 

posted on 2012-08-31 00:05  仁者无敌8勇者无惧  阅读(109)  评论(0编辑  收藏  举报

导航