SCU 4438 Censor|KMP变形题
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变形题的更多相关文章
- 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/Hash
题意:给定一个模式串和文本,要求删除所有模式串.可能删除后会形成新的模式串,必须全部删除. 思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度.这 ...
- SCU 4438 Censor(哈希+模拟栈)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...
- SCU 4438:Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- SCU 4438 Censor(Hash)题解
题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...
- Censor SCU - 4438
frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- HDU 4749-Parade Show(KMP变形)
题意: 给出一个母串和一个模式串求母串中最多能分成最大的子串数(每个字串中的各个数字的大小关系和模式串相同) 分析: KMP变形匹配规则变一下即可,用当前数字和下个数字的差表示,大小关系方便匹配 #i ...
- HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- ES6学习笔记第一天
## 三.const和let **3.1 const** const用来定义常量,所谓的常量就是值一旦给定后就不变,一次定义终身不变的量 const a = 10; a = 20; 上面的a就是一个常 ...
- 深入Java线程管理(三):线程同步
一. 引入同步: 有一个很经典的案例,即银行取款问题.我们可以先看下银行取款的基本流程: 1)用户输入账户.密码,系统判断用户的账户.密码是否匹配. 2)用户输入取款金额. 3)系统判断账户金额是否大 ...
- Python--day72--Cookie和Session内容回顾
1. Cookie是什么 保存在浏览器端的键值对 为什么要有Cookie? 因为HTTP请求是无状态的 Cookie的原理? 服务端可以在返回响应的时候 做手脚 在浏览器上写入键值对(Cookie) ...
- win10 uwp 在 Canvas 放一个超过大小的元素会不会被裁剪
我尝试在一个宽度200高度200的 Canvas 放了一个宽度 300 高度 300 的元素,这个元素会不会被 Canvas 裁剪了? 经过我的测试,发现默认是不会被裁剪 火火问了我一个问题,如果有一 ...
- 【t013】无聊的军官
Time Limit: 1 second Memory Limit: 32 MB [问题描述] 每个学年的开始,高一新生们都要进行传统的军训.今年有一个军训教官十分奇怪,他为了测试学员们的反应能力,每 ...
- 基于 Laravel-Admin 在十分钟内搭建起功能齐全的后台模板
http://laravelacademy.org/post/6468.html 1.简介 为 Laravel 提供后台模板的项目越来越多,学院君已陆续为大家介绍过Laravel Angular Ad ...
- 查看php-fpm的进程和端口号
ps -ef | grep php-fpm 查看php-fpm所有的进程 ps -ef | grep php-fpn.conf 查看配置所在路径 netstat -lntp 查看监听端口 lis ...
- Js中“==”和“===”的区别
从字面上来讲,‘==’代表相等,‘===’代表严格相等. 具体来讲,比较过程如下: 比较过程: ‘==’: 1. 首先判断两个值的类型是否相同,如果相同,进行‘===’判断. 2. ...
- Bishops Alliance—— 最大上升子序列
原题链接:http://codeforces.com/gym/101147/problem/F 题意:n*n的棋盘,给m个主教的坐标及其私有距离p,以及常数C,求位于同一对角线上满足条件:dist(i ...
- 2018-8-10-WPF-判断调用方法堆栈
title author date CreateTime categories WPF 判断调用方法堆栈 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 17: ...