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的更多相关文章

  1. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  2. SCU 4438 Censor(哈希+模拟栈)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...

  3. SCU 4438 Censor|KMP变形题

    传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...

  4. SCU 4438 Censor KMP/Hash

    题意:给定一个模式串和文本,要求删除所有模式串.可能删除后会形成新的模式串,必须全部删除. 思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度.这 ...

  5. SCU 4438 Censor(Hash)题解

    题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...

  6. Censor SCU - 4438

    frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...

  7. 四川省赛 SCU - 4438

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  8. SCU Censor

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...

  9. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

随机推荐

  1. Ubuntu 14.04 将一个sh文件制作成类似于windows下的可以双击执行的快捷方式

    # 创建文件 touch test.desktop # 在test.desktop中写入如下内容 [Desktop Entry] Version=1.0 Type=Application Termin ...

  2. fzu 2082 过路费 (树链剖分+线段树 边权)

    Problem 2082 过路费 Accept: 887    Submit: 2881Time Limit: 1000 mSec    Memory Limit : 32768 KB  Proble ...

  3. IDEA中在目录中如何快速指定到当前的类

    类似于myeclipse的 Link with Editor 其实也在IDEA的这个位置,跟狙击镜的图标一样,叫做Scroll from Source 不同的的是,IDEA的这个功能,需要手动点击,才 ...

  4. 【刷题】BZOJ 3512 DZY Loves Math IV

    Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...

  5. BZOJ 4569 [Scoi2016]萌萌哒 | ST表 并查集

    传送门 BZOJ 4569 题解 ST表和并查集是我认为最优雅(其实是最好写--)的两个数据结构. 然鹅!他俩加一起的这道题,我却--没有做出来-- 咳咳. 正解是这样的: 类似ST表有\(\log ...

  6. bzoj 4464 : [Jsoi2013]旅行时的困惑

    网络流建图. 从S向每个点连边,从每个点向T连边. 每条树边反向连一条下界为1,上界inf的边. 跑最小流. 注意加当前弧优化. #include<cstdio> #include< ...

  7. D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...

  8. 鸟哥的Linux私房菜——第九章

    视频链接,推荐看B站 土豆网:http://www.tudou.com/programs/view/XmMDbjJHJC8 B站:http://www.bilibili.com/video/av966 ...

  9. Intellij-idea 如何编译maven工程

    小编最近效应项目的要求,学习在idea上编写项目.作为一个新手遇到问题也算是正常的,重要的是把它解决,get新技能. 编写过maven工程的小伙伴们应该都知道怎么在eclipse中编译maven工程: ...

  10. js拾遗: 函数字面量

    今天落叶同学发我一篇文章,我看到一个"新"名词 "函数字面量" (也可叫直接量),当时我就郁闷了,这是什么东西? 我怎么没听说过..回头翻了下权威指南,在第 4 ...