Description

Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).

FJ has taken all of the text from the magazine to create the string S of length at most 10^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the _first_ occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

Please help FJ determine the final contents of S after censoring is complete

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。


Input

The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).
 

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.


Sample Input

whatthemomooofun
moo

Sample Output

whatthefun

Solution

之前写过这题现在重新写了一遍qwq。

研究一下这个删除就会发现如果你现在在$i$这个位置,那么你已经判断过的$1到i-1$是不会有什么影响的。

那么其实可以拿个栈来维护,栈内放字符和匹配到这个字符时再模式串中的指针的位置。

如果删除了一个数,把指针还原成当前栈顶的指针就好了。

#include <bits/stdc++.h>
using namespace std; #define ll long long
#define inf 0x3f3f3f3f
#define N 1000010 int nxt[N];
char s1[N], s2[N];
struct Stack {
char c;
int now;
}st[N]; int main() {
scanf("%s%s", s1 + , s2 + );
int n = strlen(s1 + ), m = strlen(s2 + );
for(int i = , j = ; i <= m; ++i) {
while(j && s2[j + ] != s2[i]) j = nxt[j];
if(s2[j + ] == s2[i]) ++j;
nxt[i] = j;
}
int top = ;
for(int i = , j = ; i <= n; ++i) {
while(j && s2[j + ] != s1[i]) j = nxt[j];
if(s1[i] == s2[j + ]) ++j;
st[++top] = (Stack) {s1[i], j};
if(j == m) top -= m, j = st[top].now;
}
for(int i = ; i <= top; ++i) putchar(st[i].c);
return ;
}

BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP的更多相关文章

  1. bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】

    好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...

  2. BZOJ3942 [Usaco2015 Feb]Censoring

    维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...

  3. [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】

    题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...

  4. [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)

    题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...

  5. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  6. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

  7. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  8. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  9. bzoj3940: [Usaco2015 Feb]Censoring

    AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...

随机推荐

  1. hive-site.xml配置

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-s ...

  2. clientHeight , scrollHeight , offsetHeight之间的区别

    clientHeight:元素客户区的大小,指的是元素内容及其边框所占据的空间大小(经过实践取出来的大多是视口大小) scrollHeight: 滚动大小,指的是包含滚动内容的元素大小(元素内容的总高 ...

  3. poj2524(简单并查集)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  4. ZOJ:2833 Friendship(并查集+哈希)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...

  5. Git+Jenkins+FileGee 发布php应用

    Git:做版本控制,回滚版本(coding.net) Jenkins:代码下载,提供webhook url FileGee:同步代码(一个国产同步.备份软件非常强大,而且便宜企业版只要498) jen ...

  6. Java-单向链表算法

    /** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main ...

  7. CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通 - Augusdi的专栏 - 博客频道 - CSDN.NET CUDA ...

  8. JavaScript循环练习2

    折纸:折多少次和珠穆朗玛峰一样高1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var hou = 0.0001; var cishu = 0; for(var i= ...

  9. 问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded.

    背景 今天将一个SpringBoot项目的配置参数从原有的.yml文件迁移到Apollo后,启动报错“Bean method 'rocketMQTemplate' in 'RocketMQAutoCo ...

  10. Python2 简明教程

    Python 由 Guido Van Rossum 在90年代初创建. 它现在是最流行的语言之一 我喜爱python是因为它有极为清晰的语法,甚至可以说,它就是可以执行的伪代码. 注意: 这篇文章针对 ...