SCU 4438 Censor|KMP变形题

传送门

Censor

frog is now a editor to censor so-called sensitive words (敏感词).

She has a long text P. Her job is relatively simple -- just to find the first occurence of sensitive word w 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 1 string w. The second line contains 1string p.

(1≤length of w,p≤5⋅10^6, w,p consists of only lowercase letter)

Output

For each test, write 1 string which denotes the censored text.

Sample Input

    abc
aaabcbc
b
bbb
abc
ab

Sample Output

    a

    ab

题意:给出一个单词和一段英文,要你删除你找到的第一个这个单词,并将剩下的合并成一个新的串,再继续找,直到找不出这个单词,输出最后的串。

题解:本题相当于在主串里面找模式串,找到之后删掉它然后从前往后找模式串。我们想到KMP就是用来串的模式匹配的,但是KMP和这个不一样的是KMP找到一个模式串之后直接往后面去找,而这个可能存在删除位置之前与删除位置之后拼起来组成模式串的情况。那我们怎么解决呢?我们可以用一个数组记录主串每个位置相匹配的模式串的下一个位置(next[j]),当主串匹配完一个模式串的时候将j(对应的模式串下标)跳到当前主串位置-模式串长度的位置相匹配的模式串下一位置继续比较即可。最后输出的结果字符串我们可以用一个数组在比较时记录。

代码:

#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int N = 5e6 + ;
const int INF= <<;
char s[N],t[N],ans[N];
int l1,l2,nt[N],pre[N];
void getNext() {
int i = , j = -;
nt[] = -;
while (i < l1) {
if (j == -||t[j] == t[i])
nt[++i] = ++j;
else j = nt[j];
}
}
void KMP() {
getNext();
int cnt = , i = , j = ;
for (;i<l2;i++,cnt++) {
ans[cnt] = s[i];
while (j>&&s[i] != t[j]) j = nt[j];
if (s[i] == t[j]) j++;
if (j == l1) {
cnt-=l1;
j = pre[cnt];
}
pre[cnt] = j;
ans[cnt+] = '\0';
}
printf("%s\n",ans);
}
int main() {
while (~scanf("%s%s",t,s)) {
l1 = strlen(t);
l2 = strlen(s);
KMP();
}
return ;
}

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 11 string 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

    abc
aaabcbc
b
bbb
abc
ab

Sample Output

    a

    ab
上一篇:SCU 4438:Censor


下一篇:iOS xcode工程了解