http://acm.hdu.edu.cn/showproblem.php?pid=2594
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<cstdio> #include<cstring> #include<iostream> using namespace std; char s1[50001],s2[50001]; int len1,len2,f[100001]; void getnext() { strcat(s1,s2); for(int i=1;i<len1+len2;i++) { int j=f[i]; while(j&&s1[j]!=s1[i]) j=f[j]; f[i+1]= s1[i]==s1[j] ? j+1:0; } } int main() { while(cin>>s1>>s2) { len1=strlen(s1); len2=strlen(s2); getnext(); int d=len1+len2; while(d>len1||d>len2) d=f[d]; for(int i=0;i<d;i++) printf("%c",s1[i]); if(d) printf(" "); printf("%d\n",d); } }
hdu 2594 Simpsons’ Hidden Talents
原文:http://www.cnblogs.com/TheRoadToTheGold/p/6483501.html