标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4683 Accepted Submission(s): 1702
#include <cstdio> #include <string> #include <iostream> using namespace std; int maxl[50002],p[50002]; int main() {string a,b; while(cin>>b>>a) { b=" "+b; a=" "+a; int m=b.length(); int n=a.length(); n--; m--; memset(p,0,sizeof(p)); memset(maxl,0,sizeof(maxl)); p[0]=p[1]=0; int j=0; for(int i=2;i<=m;i++) { while(j>0&&b[j+1]!=b[i])j=p[j]; if(b[j+1]==b[i])j++; p[i]=j; } j=0; for(int i=1;i<=n;i++) { while(j>0&&b[j+1]!=a[i])j=p[j]; if(b[j+1]==a[i])j++; maxl[i]=j; } if(maxl[n]==0)cout<<0<<endl; else { for(int i=1;i<=maxl[n];i++) cout<<b[i]; cout<<" "<<maxl[n]<<endl; } getchar(); } return 0; }
hdu 2594 Simpsons’ Hidden Talents KMP
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4772067.html