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 ...
随机推荐
- crm查询和删除审核历史记录
using System; using System.Linq; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; ...
- zoj 1100 - Mondriaan's Dream
题目:在m*n的地板上铺上同样的1*2的地板砖,问有多少种铺法. 分析:dp,组合,计数.经典dp问题,状态压缩. 状态:设f(i,j)为前i-1行铺满,第i行铺的状态的位表示为j时的铺砖种类数: 转 ...
- Unity异常警告错误处理方法
原地址:http://www.haogongju.net/art/2591936 1. The AnimationClip 'cube1_anim' used by the Animation co ...
- prototype 用法
prototype使得js面向对象使用了prototype之后,使用它里面的属性或者函数 需要new出一个对象才可以使用.否则不使用prototype,直接向对象注入 function Person( ...
- MATLAB 的通用命令
MATLAB 的通用命令 1.MATLAB 的标点符号及其特殊功能. 2.MATLAB 的键盘按键及其特殊功能. ↑或者Ctrl+p:调用上一次的命令 ↓或者Ctrl+n:调用下一行的命令 ←或者Ct ...
- 利用python拼接图片
问题描述: 将16张图片拼接成4x4的图片, 且不改变每单张图片的像素值. 代码如下: import os from PIL import Image path = '/share1/home/con ...
- Xcode, does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) 解决办法
在Build Settings中找到Enable Bitcode项,设置为如下: 参考:http://blog.csdn.net/soindy/article/details/48519363
- RHEL 7 安装MariaDB
RedHat Enterprise Linux 7開始.系统盘中自带的数据库为MariaDB,安装的过程例如以下: 1. 挂载安装盘镜像:mount /dev/cdrom /mnt 2. 创建yum源 ...
- 激活modelsim se 10.4 时运行patch_dll.bat不能生成TXT
问题描述: 激活modelsim时运行patch_dll.bat总是在DOS界面一闪而过,不能生成LICENSE.TXT 问题解决: 先取消文件 mgls64.dll 的只读属性(这句话在README ...
- ulimit调优|设置普通用户的ulimit值
个人总结: 如何设置普通用户的ulimit值 1.vim /etc/profile 增加 ulimit -n 10240 source /etc/profile 重新启动就不需要运行这个命令了. 2. ...