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 ...
随机推荐
- Server 非阻塞
import socket import select import Queue port =500 host = "" sock = socket.socket(socket.A ...
- Vue 状态管理 Vuex
1.概述 Vuex作为插件,管理和维护整个项目的组件状态. 2.安装vuex cnpm i --save vuex 3.vuex使用 github地址:https://github.com/MengF ...
- Oculus rift development kit 2使用手记(2014年10月到货)
昨天上午拿到后就立即拆包体验. 首先说下配置流程,其实很简单: 第一步是下载runtime,在没插dk2的时候安装好runtime. 第二步看着说明书(有图解)把硬件连接完毕.这里要说明的是dk2默认 ...
- cookie 与 session 的差别、联系
1.存放位置: Session 存放在server端. Cookie 存放在client: 2.保存形式: Session保存在server的内存中(在server端设置超时时间,与浏览器设置无关): ...
- (一)Activiti之——简介、插件安装及BPMN元素
1. 工作流概念 工作流(Workflow):就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现 ...
- <转> lua: userdata的metatable使用
1 如何封装c++的指针 对于c++对象的lua包装,我们可以使用 template<typename T> struct luaUserdataWrapper { luaUserdat ...
- 【MyBatis】MyBatis分页插件PageHelper的使用
好多天没写博客了,因为最近在实习,大部分时间在熟悉实习相关的东西,也没有怎么学习新的东西,这周末学习了MyBatis的一个分页插件PageHelper,虽然没有那么的强大(我在最后会说明它的缺点),但 ...
- Python技术公众号100天了
公众号100天了,是个值得一提的日子! 我从2017年10月31日开始做这个公众号,到今天2018年2月7日,差不多100天时间 .虽然公众号很早就申请了,但直到去年10月31日,我才有真正把这个公众 ...
- Atitit.nosql api 标准化 以及nosql数据库的实现模型分类差异
Atitit.nosql api 标准化 以及nosql数据库的实现模型分类差异 1. 常用的nosql数据库MongoDB Cassandra1 1.1. 查询> db.blogposts. ...
- Effective C++:条款22:将成员变量声明为private
(一)为什么不採用public成员变量 (1)首先,从语法一致性考虑,客户唯一能訪问对象的方法就是通过成员函数,客户不必考虑是否该记住使用小括号(). (2)其次,使用函数能够让我们对成员变量的处理有 ...