首页 技术 正文
技术 2022年11月10日
0 收藏 674 点赞 5,034 浏览 1835 个字

传送门

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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295