前面一大串的英文题面被我忽略了
KMP+栈
只需通过维护一个栈就可以了(* ̄︶ ̄)(我懒得多写)
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdlib> 7 using namespace std; 8 char s1[1000005],s2[1000005],s[1000005]; 9 int n,m,p[1000005],next[1000005],top; 10 int main() 11 { 12 gets(s1+1); gets(s2+1); 13 n=strlen(s1+1); m=strlen(s2+1); 14 p[1]=0; int j=0; 15 for (int i=1; i<=n; i++){ 16 while (j>0 && s2[j+1]!=s2[i+1]) j=p[j]; 17 if (s2[j+1]==s2[i+1]) j++; 18 p[i+1]=j; 19 } 20 next[0]=0; 21 for (int i=1; i<=n; i++){ 22 s[++top]=s1[i]; 23 int j=next[top-1]; 24 while (j>0 && s2[j+1]!=s[top]) j=p[j]; 25 if (s2[j+1]==s[top]) j++; 26 next[top]=j; 27 if (next[top]==m) top-=m; 28 } 29 for (int i=1; i<=top; i++) putchar(s[i]); putchar('\n'); 30 return 0; 31 }
miao~~~