HIT暑期集訓Day8 字串
阿新 • • 發佈:2020-08-08
一個網上找到關於馬拉車演算法的部落格
https://www.jianshu.com/p/392172762e55
C HDU 4300
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 100100 using namespace std; int p[maxn]; char a[maxn],b[maxn],c[maxn],d[maxn]; void prework(int len) { p[0]=-1; int i,j=-1; for(i=1;i<len;i++) { while (j>=0 && b[j+1]!=b[i]) j=p[j]; if (b[j+1]==b[i]) j++; p[i]=j; } } int work(int lim,int len) { p[0]=-1; int i,j=-1; for (i=lim;i<len;i++) { while (j>=0 && b[j+1]!=a[i]) j=p[j]; if (b[j+1]==a[i]) j++; } return j; } int main() { int t,i,j,k,len,lc,lim,re; scanf("%d",&t); while (t--) { memset(p,0,sizeof(p)); scanf("%s%s",c,a); lc=strlen(c); len=strlen(a); for (i=0;i<lc;i++) d[c[i]-'a']=i+'a'; for (i=0;i<len;i++) b[i]=d[a[i]-'a']; prework(len); lim=(len+1)/2; re=work(lim,len); for (i=0;i<len-re-1;i++) printf("%c",a[i]); for (i=0;i<len-re-1;i++) printf("%c",b[i]); printf("\n"); } return 0; }
D HDU 4821
E HDU 5340