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 ...
随机推荐
- Python的数据结构
目录 Python内置的数据结构 序列Sequence 映射Mapping 集合Sets Python内置的数据结构 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python内置的 ...
- CF375D Tree and Queries
题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...
- MT【113】无中生有加一个减一个
代数上可以这么解答:不妨设$x\le y$ 1)若$y-x\le\frac{1}{2},则|f(x)-f(y)|<\frac{1}{2}|x-y|\le\frac{1}{4}$ 2)若$y-x& ...
- [Swerc2014 C]Golf Bot
题意:给你N个数字,每次利用这N个数字中最多两个数字进行加法运算,来得到目标中的M个数字. Solution: 我们先来看看多项式乘法:\(A(x)=\sum_{i=0}^{n-1}a_ix^i\), ...
- 洛谷P3935 Calculating(整除分块)
题目链接:洛谷 题目大意:定义 $f(x)=\prod^n_{i=1}(k_i+1)$,其中 $x$ 分解质因数结果为 $x=\prod^n_{i=1}{p_i}^{k_i}$.求 $\sum^r_{ ...
- 【bzoj2961】 共点圆
http://www.lydsy.com/JudgeOnline/problem.php?id=2961 (题目链接) 题意 按照一定的顺序给出一些圆和一些点,对于每一个点问是否在所有圆内. Solu ...
- java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项
先看一下下面的结果 1.System.out.println(127==127); //true , int type compare 2.System.out.println(128==128); ...
- Nginx反向代理2--配置文件配置
2.1Nginx的反向代理 什么是正向代理? 1.2 使用nginx实现反向代理 Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请 ...
- Elasticsearch 5.0 安装 Search Guard 5 插件
一.Search Guard 简介 Search Guard 是 Elasticsearch 的安全插件.它为后端系统(如LDAP或Kerberos)提供身份验证和授权,并向Elasticsearc ...
- python 获取自身ip
原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- ...