Censor SCU - 4438

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf = 0x3f3f3f3f;
 4 typedef long long int ll;
 5 typedef unsigned long long int ull;
 6 const int maxn = 5e6+10;
 7 ull hs[maxn];
 8 ull base[maxn];
 9 char stra[maxn], strb[maxn];
10 int lena,lenb;
11 ull num;
12 char ans[maxn];
13 int main() {
14     while (scanf("%s%s", stra + 1,strb+1) != EOF) {
15         lena = strlen(stra + 1);
16         lenb = strlen(strb + 1);//读入串 
17         num = 0;
18         base[0] = 1;
19         for (int i = 1;i < maxn;i++)base[i] = base[i - 1] * 131;
20         for (int i = 1;i <= lena;++i)num = (num * 131) + stra[i] - 'a' + 1;//初始化 
21         int pos = 0;
22         for (int i = 1;i <= lenb;++i) {
23             ans[pos++] = strb[i];
24             hs[pos] = hs[pos - 1] * 131 + strb[i] - 'a' + 1;
25             if (pos >= lena && hs[pos] - hs[pos - lena] * base[lena] == num) {//如果长度大于子串就判断,如果等于子串,pos就减掉子串的长度。 
26                 pos -= lena;
27             }
28         }
29         for (int i = 0;i < pos;i++) printf("%c", ans[i]);
30         puts(""); 
31     }
32     return 0;
33 }

 

posted @ 2020-07-25 16:11  programmer_w  阅读(409)  评论(0编辑  收藏  举报