BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 404  Solved: 221
[Submit][Status][Discuss]

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

HINT

Source

Silver

分析:

就是维护一个栈,然后如果栈顶出现了t,弹出t,继续匹配,用kmp加速匹配过程...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=1000000+5;

int n,m,tail,nxt[maxn],stk[maxn];

char s[maxn],t[maxn],ans[maxn];

inline void getnxt(void){
	nxt[0]=nxt[1]=0;int k;
	for(int i=1;i<n;i++){
		k=nxt[i];
		while(k&&t[k+1]!=t[i+1])
			k=nxt[k];
		if(t[k+1]==t[i+1])
			nxt[i+1]=k+1;
		else
			nxt[i+1]=0;
	}
}

inline void kmp(void){
	tail=0;int i=1;
	for(int i=1;i<=n;i++){
		ans[++tail]=s[i];
		int j=stk[tail-1];
		while(j&&t[j+1]!=s[i])
			j=nxt[j];
		if(s[i]==t[j+1])
			j++;
		stk[tail]=j;
		if(j==m)
			tail-=m;
	}
}

signed main(void){
	scanf("%s%s",s+1,t+1);
	n=strlen(s+1);m=strlen(t+1);
	getnxt();kmp();
	for(int i=1;i<=tail;i++)
		printf("%c",ans[i]);puts("");
	return 0;
}

  



By NeighThorn

时间: 2024-10-14 00:14:21

BZOJ 3942: [Usaco2015 Feb]Censoring的相关文章

BZOJ 3942 Usaco2015 Feb Censoring KMP算法

题目大意:给定两个串A和B,要求将A中删掉所有的B后输出 为何BC群刚有人问完我这题的[C++语法基础题]版之后就出了个KMP版的= = 维护一个栈,将A中的字符依次加进去,一旦A的栈顶出现了B就弹栈 用KMP算法来加速这个过程即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 using namespace st

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB 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

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset

Bzoj3940 [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 391  Solved: 183 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

[Usaco2015 Feb]Censoring

A. Censoring 题目描述 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 rathe

【bzoj3940】[Usaco2015 Feb]Censoring

[题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词. FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词.他重复这个操作直到S中 没有列表里的单词为止.注意删除一个单词后可能会导致S中出现另一个列表中的单词 FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是

BZOJ 3943 Usaco2015 Feb SuperBull Prim

题目大意:给定n个数,每次选择两个数,将两数的异或值计入答案,并删掉其中一个,反复如此直到只剩一个数为止,求答案的最大值 每次将选择的两个数连边,那么显然会得到一棵树 用Prim算法求最大生成树即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 2020 using namespace std; int n,a[M]; long

bzoj3943[Usaco2015 Feb]SuperBull*

bzoj3943[Usaco2015 Feb]SuperBull 题意: n头牛进行锦标赛,每场比赛的好看程度是两头牛的编号异或和,并总有一方被淘汰.求安排比赛(可以决定比赛胜负)可以得到的最大总好看程度是多少.n≤2000 题解: 先求出牛两两之间的异或和,然后发现可以把比赛看做连边,且共有n-1场比赛,所以求最大生成树就行了.神犇们用的都是Prim,蒟蒻不会,用Kruscal结果时间排倒数. 代码: 1 #include <cstdio> 2 #include <cstring>

BZOJ 3670 &amp;&amp; BZOJ 3620 &amp;&amp; BZOJ 3942 KMP

最近感到KMP不会啊,以前都是背板的现在要理解了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxn=16000; 7 8 char S[Maxn]; 9 int k,P[Maxn],Ans; 10 11 12 inline void Kmp(char * S