HDU 1867 KMP
生活随笔
收集整理的這篇文章主要介紹了
HDU 1867 KMP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
求str1?的最長后綴與 str2 的最長前綴。使得 str1+str2? 的長度最小,并且字典序最小(str1和str2可以互換)
?
題解:
kmp的p數組的含義:p[i]表示以i為結尾的字符串最多和開頭匹配的個數。也正是這道題求解的關鍵、
具體做法就是將兩個字符串合并處理求一下p數組就好了~
ps:合并的時候中間一定要加“分隔符”(比如:#,@之類的~),否則會有驚喜。。。
abcbcbca bcbcbc ? ?對拍了半天才發現這個bug。。。
?
View Code 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <algorithm> 6 7 #define N 2001000 8 9 using namespace std; 10 11 int lena,lenb,lenc; 12 int p[N]; 13 char str1[N],str2[N],c[N]; 14 15 inline void getp() 16 { 17 p[1]=0; 18 for(int i=2,len=0;i<=lenc;i++) 19 { 20 while(len>0&&c[i]!=c[len+1]) len=p[len]; 21 if(c[i]==c[len+1]) len++; 22 p[i]=len; 23 } 24 } 25 26 inline int getlen(char a[],char b[]) 27 { 28 lena=strlen(a+1); 29 lenb=strlen(b+1); 30 lenc=lena+lenb+1; 31 for(int i=1;i<=lenb;i++) c[i]=b[i]; 32 c[1+lenb]='#'; 33 for(int i=1;i<=lena;i++) c[i+lenb+1]=a[i]; 34 getp(); 35 return p[lenc]; 36 } 37 38 inline void go() 39 { 40 int len1=getlen(str1,str2); 41 int len2=getlen(str2,str1); 42 if(len1==len2) 43 { 44 if(strcmp(str1+1,str2+1)<0) printf("%s%s",str1+1,str2+len1+1); 45 else printf("%s%s",str2+1,str1+len2+1); 46 } 47 else if(len1<len2) printf("%s%s",str2+1,str1+len2+1); 48 else printf("%s%s",str1+1,str2+len1+1); 49 puts(""); 50 } 51 52 int main() 53 { 54 while(scanf("%s%s",str1+1,str2+1)!=EOF) go(); 55 return 0; 56 }?
?
轉載于:https://www.cnblogs.com/proverbs/archive/2013/02/05/2892762.html
總結
以上是生活随笔為你收集整理的HDU 1867 KMP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 监控自定义信息 —— ESFramewo
- 下一篇: 服务器 发布 WebService 错误