BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP
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
Output
The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
Sample Input
moo
Sample Output
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的更多相关文章
- bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】
好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...
- BZOJ3942 [Usaco2015 Feb]Censoring
维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...
- [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】
题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...
- [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)
3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- 3942: [Usaco2015 Feb]Censoring
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...
- bzoj3940: [Usaco2015 Feb]Censoring
AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...
随机推荐
- list的方法、操作
序号 分类 关键字 / 函数 / 方法 说明 1 增加 列表.insert(索引, 数据) 在指定位置插入数据 列表.append(数据) 在末尾追加数据 列表.extend(列表2) ...
- 模仿linux内核定时器代码,用python语言实现定时器
大学无聊的时候看过linux内核的定时器,如今已经想不起来了,也不知道当时有没有看懂,如今想要模仿linux内核的定时器.用python写一个定时器,已经想不起来它的设计原理了.找了一篇blog,li ...
- latex 转word
1:下载pandoc软件,支持多种文件格式互转. http://www.pandoc.org/installing.html#windows 2:下载zip包,解压,并将含有pandoc.exe的目录 ...
- 深入理解python之二——python列表和元组
从一开始学习python的时候,很多人就听到的是元组和列表差不多,区别就是元组不可以改变,列表可以改变. 从数据结构来说,这两者都应当属于数组,元组属于静态的数组,而列表属于动态数组.稍后再内存的分配 ...
- [LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- H2O.ai初步使用
1.官网下载最新稳定版,https://www.h2o.ai/download/ ,如果点击下载无反应,请使用ie浏览器 2.解压h2o-3.18.0.10.zip到目录h2o-3.18.0.10 3 ...
- http协议基础(八)请求首部字段
请求首部字段 定义:请求首部字段是从客户端到服务器发送请求报文中所使用的字段,里面包含了附加信息.客户端信息以及对响应内容相关的优先级等内容 1.Accept 通知服务器用户代理可处理的媒体类型及媒体 ...
- Summary: Binary Search
Iterative ways: int binarySearch (int[] a, int x) { int low = 0; int high = a.length - 1; int mid; w ...
- 倒计时60s
- POJ 1836
刚开始二分写错了 wa了很久 这个二分 的好好想想 #include <iostream> #include<cstdio> #include<string.h> ...