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 ...
随机推荐
- word中拷贝图片到matlab
完全可以! step1:在wod用鼠标右键复制 step2:打开附件-画图,点工具栏-编辑-粘贴(或ctl+v) step3:在MATLAB中调用A = imead(filename) 例如A=ime ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- Java-使用IO流对大文件进行分割和分割后的合并
有的时候我们想要操作的文件很大,比如:我们想要上传一个大文件,但是收到上传文件大小的限制,无法上传,这是我们可以将一个大的文件分割成若干个小文件进行操作,然后再把小文件还原成源文件.分割后的每个小文件 ...
- MongoDB 工具助手类(.NET)
在开发过程中,需要用到MongoDB,本身MongoDB自己对类的封装就特别好了.为了更加符合我们平时的开发使用,我现在进行了一个简单的封装操作. 连接数据库类:MongoDBContext usin ...
- Ants-穷举算法
package java操作excel; import java.util.Scanner; /** * Ants * n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行,当蚂蚁爬到端点就会掉下去,竿 ...
- bootstrap 带有确定取消按钮的modal
</div><div class="modal fade" id="confirmModal" tabindex="-1" ...
- mysql的Navicat查看数据库的ER图
1.mysql数据库表间的关系图可以通过navicat查看.
- discuz财付通也阵亡了
今日做交易部分,然后焦头烂额. 首先这积分,威望,金钱,什么鬼,乱七八糟的...... 然后这支付宝,啊,,,,,竟然停止个人接口了,不得已要使用财付通. %&……*&……&不 ...
- AVPlayerLayer
AVPlayerLayer 最后一个图层类型是AVPlayerLayer.尽管它不是Core Animation框架的一部分(AV前缀看上去像),AVPlayerLayer是有别的框架(AVFound ...
- git提交时候出错
Please make sure you have the correct access rights and the repository exists. 解决方案: 主要原因是没有加载keygen ...