C. Censor
Time Limit: 2000ms
Memory Limit: 65536KB
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
1
string
p
.
(
1≤length of w,p≤5⋅106
,
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
题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6865#problem/C
题目大意:一个子串,一个母串,要求删除母串中的子串,删完剩余部分合并继续删,直到母串中不存在子串为止,输出此时的母串
题目分析:不要求合并的话直接kmp即可,现在删完要合并,其实只要记录每次删完,指向子串的指针回溯到哪个位置即可
#include <cstdio>
#include <cstring>
int const MAX = 5e6 + 5;
char s1[MAX], s2[MAX], ans[MAX];
int next[MAX], pos[MAX];
int l1, l2, cnt;
void get_next()
{
int i = 0, j = -1;
next[0] = -1;
while(s2[i] != '\0')
{
if(j == -1 || s2[i] == s2[j])
{
i ++;
j ++;
if(s2[i] == s2[j])
next[i] = next[j];
else
next[i] = j;
}
else
j = next[j];
}
}
void KMP()
{
get_next();
int i = 0, j = 0;
cnt = 0;
while(s1[i] != '\0')
{
ans[cnt] = s1[i ++];
while(!(j == -1 || ans[cnt] == s2[j]))
j = next[j];
j ++;
cnt ++;
pos[cnt] = j;
if(j == l2)
{
cnt -= l2;
j = pos[cnt];
}
}
}
int main()
{
while(scanf("%s %s", s2, s1) != EOF)
{
l1 = strlen(s1);
l2 = strlen(s2);
KMP();
for(int i = 0; i < cnt; i++)
printf("%c", ans[i]);
printf("\n");
}
}