SCU 4438:Censor
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 1 string p
.
(1≤length of w,p≤5⋅106
, 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
题意: 给你一个主串,递归删除模式串。
比如: T: abc S: aaabcbc
aaabcbc->aabc->a
非常巧妙的KMP,我们用一个栈记录当前的字符以及其在模式串匹配的位置,当位置等于模式串长度之后,将模式串长度的串出栈,从栈顶元素开始继续匹配主串.时间复杂度 O(n).
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
using namespace std;
const int N = ;
struct Node{
char c;
int k;
};
char w[N],t[N],ans[N];
int Next[N];
void get_next(char *p){
int len = strlen(p); int i=,k=-;
Next[] = -;
while(i<len){
if(k==-||p[i]==p[k]){
i++,k++;
Next[i] = k;
}
else k = Next[k];
}
} void Kmp(char *s,char *p){
int len1 = strlen(s),len2 = strlen(p);
int i=,j=,len;
stack <Node> stk;
while(!stk.empty()) stk.pop();
while(i<len1){
if(j==-||s[i]==p[j]){
i++,j++;
stk.push(Node{s[i-],j});
}else {
j=Next[j];
}
if(j==len2){
len = len2;
while(!stk.empty()&&len--) stk.pop();
if(stk.empty()) j = ;
else j = stk.top().k;
}
}
int k = ;
while(!stk.empty()){
ans[k++] = stk.top().c;
stk.pop();
}
for(int i=k-;i>=;i--) printf("%c",ans[i]);
printf("\n");
}
int main(){
while(scanf("%s%s",w,t)!=EOF){
get_next(w);
Kmp(t,w);
}
return ;
}
SCU 4438:Censor的更多相关文章
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- 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|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字符和未删除的字符能匹配模式串的长度.这 ...
- 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 ...
- 四川省赛 SCU - 4438
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- SCU Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
随机推荐
- 深入理解Vue的生命周期
谈到Vue的生命周期,相信许多人并不陌生.但大部分人和我一样,只是听过而已,具体用在哪,怎么用,却不知道.我在学习vue一个多礼拜后,感觉现在还停留在初级阶段,对于mounted这个挂载还不是很清楚. ...
- pgm6
有个比较有意思的想法是编码理论的反问题是 machine learning,这也是这部分学习的一个收获.这个其实很奇怪,编码理论其实是有 ground truth 的,然后通过编码产生“冗余”,这样才 ...
- python_面向对象小试题
打印啥? class Animal(object): hobby = "eat" def run(self): print(self.hobby) return self.hobb ...
- 内置函数enumerate()
enumerate是枚举的意思,顾名思义,enumerate()函数用来将一个可迭代序列生成一个enumerate对象,该enumerate对象的每个元素是由可迭代对象的索引编号和对应的元素组成的元祖 ...
- 基于Maven构建的Spring+Mybatis项目
项目的目录结构: 1.基于Maven构建Web项目 参考:基于Maven构建Web项目 2.导入项目依赖 Spring 核心容器(Beans.Core.Context.Context support. ...
- 图像处理之生成ColorBar
1 colorBar介绍 colorBar主要是指一些图像处理中使用的常见纯色或者渐变色条.colorBar用途可作为测试样图来验证某些图像算法的效果,从而避免图像内容或者硬件对图像的干扰,使图像算法 ...
- Linux 服务器上快速配置阿里巴巴 OPSX NTP服务
编辑文件 "/etc/ntp.conf",根据情况修改文件内容为: 互联网上的服务器: driftfile /var/lib/ntp/drift pidfile /var/run/ ...
- Python中的requests模块注意事项
主要是说requests.post()方法, 参数: url : 这就不解释了 data: 如果传入的是字典类型,则字典在发出请求时会自动编码为表单形式,表单形式会将字典中的键和值进行一些操作: ...
- 样本标准差分母为何是n-1
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- Hadoop生态圈-Azkaban部署实战
Hadoop生态圈-Azkaban部署实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Azkaban部署流程 1>.上传azkaban程序并创建解压目录 [yinz ...