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 ...
随机推荐
- sql server 基本问题解决思路
1.数据库故障排查步骤,如何处理紧急数据库问题;首先根据报错信息找到故障原因.然后实施对应的解决方案.2.SQL调优步骤,如何来判断SQL语句存在问题,怎么定位问题,如何解决这些问题:可以建立一个Pe ...
- sql server中的日期详解使用(convert)
转自:http://blog.csdn.net/hehe520347/article/details/48496853 有个字段值例如2012-07-02 00:00:00.000 转化成 2012- ...
- HDU 1068 Girls And Boys 二分图题解
版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- qsv转mp4
1:下载格式工厂:http://rj.baidu.com/soft/detail/13052.html?ald 2:安装 :选择安装位置,把不需要安装的软件前面的对号去掉. 3:下一步,把不需要的软件 ...
- [py][mx]django邮箱注册的验证码部分-django-simple-captcha库使用
邮箱注册-验证码 验证码使用第三方库django-simple-captcha 这个安装图形插件步骤官网有哦 - 1.Install django-simple-captcha via pip: pi ...
- python输入与输出165
s = 'Hello,Runoob' print(s) str(s) print(s) print(repr(s)) print(1/7) print(str(1/7)) print(repr(1/7 ...
- testNG入门详解
TestNG 的注释: @DataProvider @ExpectedExceptions @Factory @Test @Parameters <suite name="Parame ...
- react native android 编译
修改 Maven 仓库地址 React Native 在初始化时会从 jcenter.binary.com 这个地方下载一些东西,网上搜索了一下,好像是在下载 Maven 相关的依赖. 针对全局进行修 ...
- Genymotion虚拟镜像下载慢或者失败的解决办法
Genymotion虚拟镜像下载慢或者失败的解决办法 http://files2.genymotion.com/dists/8.0.0/ova/genymotion_vbox86p_8.0_18061 ...
- 20154312 曾林 EXP7 网络欺诈防范
目录 1.基础问题回答 ----1.1.通常在什么场景下容易受到DNS spoof攻击 ----1.2.在日常生活工作中如何防范以上两攻击方法 2.实践总结与体会 3.实践过程记录 ----3.1.简 ...