传送门

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|KMP变形题的更多相关文章

  1. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  2. SCU 4438 Censor KMP/Hash

    题意:给定一个模式串和文本,要求删除所有模式串.可能删除后会形成新的模式串,必须全部删除. 思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度.这 ...

  3. SCU 4438 Censor(哈希+模拟栈)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...

  4. SCU 4438:Censor

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...

  5. SCU 4438 Censor(Hash)题解

    题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...

  6. Censor SCU - 4438

    frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...

  7. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  8. HDU 4749-Parade Show(KMP变形)

    题意: 给出一个母串和一个模式串求母串中最多能分成最大的子串数(每个字串中的各个数字的大小关系和模式串相同) 分析: KMP变形匹配规则变一下即可,用当前数字和下个数字的差表示,大小关系方便匹配 #i ...

  9. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. theadClasses设置Bootstrap Table表头样式

    通过theadClasses属性设置表头样式. thead-light设置灰色背景 //bootstrap table初始化数据 itxst.com $('#table').bootstrapTabl ...

  2. Fragment学习(一) :生命周期

    一. 何为Fragment Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个Activity中来创建一个多面界面,并且你可以在多个Activity ...

  3. Python--day22--初识面向对象

  4. Codeforces Round #190 (Div. 1 + Div. 2)

    A. Ciel and Dancing 模拟. B. Ciel and Flowers 混合类型的数量只能为0.1.2,否则3个可以分成各种类型各自合成. C. Ciel and Robot 考虑一组 ...

  5. pytorch中如何处理RNN输入变长序列padding

    一.为什么RNN需要处理变长输入 假设我们有情感分析的例子,对每句话进行一个感情级别的分类,主体流程大概是下图所示: 思路比较简单,但是当我们进行batch个训练数据一起计算的时候,我们会遇到多个训练 ...

  6. 2019-9-2-给博客添加rss订阅

    title author date CreateTime categories 给博客添加rss订阅 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17:23 ...

  7. hadoop的6个进程启动不全,请试 比如datanode没有启动

      赋给hadoop用户hadoop-1.2.1读写权限 [root@master usr]# chown -R hadoop121:hadoop121 hadoop-1.2.1/   折腾了两个小时 ...

  8. Github开源人脸识别项目face_recognition

    Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...

  9. P1006 输出第二个整数

    题目描述 输入三个整数,整数之间由一个空格分隔,整数是32位有符号整数.把第二个输入的整数输出. 输入格式 输入三个整数,整数之间由一个空格分隔,整数是32位有符号整数. 输出格式 输出输入的三个整数 ...

  10. asp dotnet core 图片在浏览器没访问可能原因

    我写了一个项目用来广告就用到广告的图片,但是广告的图片放在博客的链接无法访问,连我的方法都没有调用,而我尝试网页直接访问图片链接是可以访问的,最后找到原因是广告插件禁用了图片访问 我在一个方法创建了广 ...