Censor(KMP)
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
// 题意:第一行一个敏感词,然后一行文本,操作如下,删除敏感词,然后如果又新组成敏感词, 重复删掉,直到没有敏感词
//如果用KMP和模拟链表的话,就是如果匹配成功了,用链表删除后,再回退lent长度继续匹配,有点麻烦,貌似还可以hash做
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
using namespace std;
const int MX=; int lens,lent;
char T[MX];
char S[MX];
int l[MX];
int r[MX];
int fail[MX]; void getfail()
{
int i,j;
i=,j=;
fail[]=;
while(i<lent)
{
if (j==||T[i]==T[j])
{
i++,j++;
fail[i]=j;
}
else j = fail[j];
}
} void kmp()
{
int i=;
int j=;
while(i<=lens&&j<=lent)
{
if (j==||S[i]==T[j])
{
i=r[i];
j++;
}
else j = fail[j]; if (j>lent)
{
int num=;
int pos=i;
while (num<lent)
{
pos = l[pos];
num++;
}
r [ l[ pos ] ] = i;
l [ i ] = l[ pos ]; num=;
pos=i;
while (num<lent)
{
if (l[pos]==) break;
pos = l[pos];
num++;
}
j=;
i = pos;
}
}
} int main()
{
while(scanf("%s",T+)!=EOF)
{
scanf("%s",S+);
lens = strlen(&S[]);
lent = strlen(&T[]); getfail(); for (int i=;i<=lens+;i++)
{
l[i] = i-;
r[i] = i+;
} kmp();
for (int i = r[];S[i];i = r[i])
{
printf("%c",S[i]);
}
printf("\n");
}
return ;
}
Censor(KMP)的更多相关文章
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- SCU 4438 Censor|KMP变形题
传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...
- SCU 4438 Censor KMP/Hash
题意:给定一个模式串和文本,要求删除所有模式串.可能删除后会形成新的模式串,必须全部删除. 思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度.这 ...
- 2015 四川省赛 C Censor(哈希 | KMP)
模式串为子串 KMP /* @author : victor */ #include <bits/stdc++.h> using namespace std; typedef long l ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- 『字符串模式匹配 KMP』
字符串模式匹配 我们要先了解一下问题是什么. 模式匹配是数据结构中字符串的一种基本运算,给定一个子串,要求在某个字符串中找出与该子串相同的所有子串,这就是模式匹配. KMP 然后我们来认识一下今天的主 ...
- Censor SCU - 4438
frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...
- SCU 4438:Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
随机推荐
- Python学习笔记(六)多进程实现并发服务器
这个相对于多进程更加简单,每accept一个新的连接就创建一个新的线程.代码如下: # coding: utf-8 import socket import sys import errno impo ...
- IIC读写AT24C02代码2——串口命令控制多页读写
通过串口输入 R .W 进行控制程序读写IIC设备.波特率9600bps,晶振115200HZ. main.c /*------------------------------------------ ...
- mybatis 自动更新表结构 ,兼容通用tkmapper
1.maven引入jar <dependency> <groupId>com.github.gonglb.tools</groupId> <artifactI ...
- A.0 B.1 C.2 D.3
17. 以下哪个不是广告平台? A.Admob B.Domob C.InMobi D.TalkingData 错误 应该选择:D.TalkingData 10. 哪个不是免费的工具? A.Xcode ...
- java学习路线-Java技术人员之路从0基础到高级
满满的 全是干货 java基础: 尚学堂 马士兵 个人推荐 历经5年锤练--史上最适合刚開始学习的人入门的Java基础视频 很具体 适合 时间多的看 传智播客java基础班 马士兵线程 ...
- Jpa调用存储过程及参数
public List<Object> findAllEntityListBySearch(Long inputInfoId, int flag) throws Exception { L ...
- 借鉴炉石传说的战棋游戏《DarkWar》
<炉石传说>是现在很火的休闲对战游戏,本人也非常喜欢玩,玩的时候经常想能不能把炉石的这些元素融入到战棋类游戏中,于是思索良久,又恰逢游戏蛮牛开展第三届蛮牛杯游戏开发大赛,于是用Unity3 ...
- unity, inspector debug
以前经常因为脚本中private变量不在inspector界面中显示,不方便观察其值的变化,所以本该用private的用了public. 今天发现,原来inspector有个选项,如图,平常勾选的是N ...
- NIO之Channel聚集(gather)写入与分散(scatter)读取
Channel聚集(gather)写入 聚集写入( Gathering Writes)是指将多个 Buffer 中的数据“聚集”到 Channel. 特别注意:按照缓冲区的顺序,写入 position ...
- Atitit.编程语言and 自然语言的比较and 编程语言未来的发展
Atitit.编程语言and 自然语言的比较and 编程语言未来的发展 1. 单词的间隔靠空格,编程的单词的间隔靠分界符..1 2. 语句分界符:自然语言使用逗号,编程语言使用分号1 3. 换行1 4 ...