SCU - 4438 Censor
生活随笔
收集整理的這篇文章主要介紹了
SCU - 4438 Censor
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Censor
frog is now a editor to censor so-called sensitive words (敏感詞).
She has a long text?pp. Her job is relatively simple -- just to find the first occurence of sensitive word?ww?and remove it.
frog repeats over and over again. Help her do the tedious work.
Input
The input consists of multiple tests. For each test:
The first line contains?11?string?ww. The second line contains?11string?pp.
(1≤length of?w,p≤5?1061≤length of?w,p≤5?106,?w,pw,p?consists of only lowercase letter)
Output
For each test, write?11?string which denotes the censored text.
Sample Input
abcaaabcbcbbbbabcabSample Output
aab這題用KMP可以寫 但是KMP不會
但是用HASH暴力一下,HASH暴力出奇跡
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string> 10 #include <math.h> 11 #include <stdlib.h> 12 #include <time.h> 13 using namespace std; 14 typedef unsigned long long ull; 15 const int maxn = 5e6 + 10; 16 const int seed = 13331; 17 ull HASH, s[maxn], p[maxn]; 18 char a[maxn], b[maxn], ans[maxn]; 19 void init() { 20 p[0] = 1; 21 for (int i = 1 ; i < maxn ; i++) 22 p[i] = p[i - 1] * seed; 23 } 24 int main() { 25 init(); 26 while(scanf("%s%s", a, b ) != EOF ) { 27 int lena = strlen(a), lenb = strlen(b); 28 if (lena > lenb) { 29 printf("%s\n", b); 30 continue; 31 } 32 HASH = 0; 33 for (int i = 0 ; i < lena ; i++) 34 HASH = HASH * seed + a[i]; 35 int top = 0; 36 s[0] = 0; 37 for (int i = 0 ; i < lenb ; i++) { 38 ans[top++] = b[i]; 39 s[top] = s[top - 1] * seed + b[i]; 40 if ( top >= lena && s[top] - s[top - lena]*p[lena] == HASH ) top -= lena; 41 } 42 for (int i = 0 ; i < top ; i++) 43 printf("%c", ans[i]); 44 printf("\n"); 45 } 46 return 0; 47 }
?
轉載于:https://www.cnblogs.com/qldabiaoge/p/9152695.html
總結
以上是生活随笔為你收集整理的SCU - 4438 Censor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces 785 D.Ant
- 下一篇: SpringBoot——项目搭建、整合M