标签:cee sel kmp using mem ted key chm des
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7978 Accepted Submission(s): 2005
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int N = 2e5+10; int Next[N]; char S[N], T[N]; char a[N],b[N]; char tmp[N]; char res1[N],res2[N]; int slen, tlen;//注意每次一定要计算长度 int alen,blen; int ans; int len1,len2; void getNext() { int j, k; j = 0; k = -1; Next[0] = -1; while(j < tlen) if(k == -1 || T[j] == T[k]) Next[++j] = ++k; else k = Next[k]; } int main() { while(scanf("%s%s",a,b)!=EOF){ alen=strlen(a); blen=strlen(b); for(int i=0;i<blen;i++) T[i]=b[i]; T[blen]=‘#‘; for(int i=0;i<alen;i++) T[blen+1+i]=a[i]; tlen=alen+blen+1; T[tlen]=0; getNext(); ans=Next[tlen]; for(int i=0;i<alen;i++) res1[i]=a[i]; for(int i=ans;i<blen;i++) res1[alen+i-ans]=b[i]; len1=alen+blen-ans; res1[len1]=0; memcpy(tmp,a,sizeof(a)); memcpy(a,b,sizeof(b)); memcpy(b,tmp,sizeof(tmp)); swap(alen,blen); for(int i=0;i<blen;i++) T[i]=b[i]; T[blen]=‘#‘; for(int i=0;i<alen;i++) T[blen+1+i]=a[i]; tlen=alen+blen+1; T[tlen]=0; getNext(); ans=Next[tlen]; for(int i=0;i<alen;i++) res2[i]=a[i]; for(int i=ans;i<blen;i++) res2[alen+i-ans]=b[i]; len2=alen+blen-ans; res2[len2]=0; if(len1<len2) printf("%s\n",res1); else if(len1>len2) printf("%s\n",res2); else { if(strcmp(res1,res2)<=0) printf("%s\n",res1); else printf("%s\n",res2); } } return 0; }
HDU1867 A + B for you again(KMP)
标签:cee sel kmp using mem ted key chm des
原文地址:http://www.cnblogs.com/a249189046/p/7447354.html