[Usaco2015 Feb]Censoring
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^5 characters. He has a list of censored words t_1 ... t_N that he wishes to delete from S. To do so Farmer John finds the earliest occurrence of a censored word in S (having the earliest start index) and removes that instance of the word from S. He then repeats the process again, deleting the earliest occurrence of a censored word from S, repeating until there are no more occurrences of censored words in S. Note that the deletion of one censored word might create a new occurrence of a censored word that didn't exist before.Farmer John notes that the censored words have the property that no censored word appears as a substring of another censored word. In particular this means the censored word with earliest index in S is uniquely defined.Please help FJ determine the final contents of S after censoring is complete.
FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S。他有一个包含n个单词的列表,列表里的n个单词记为t_1...t_N。他希望从S中删除这些单词。
FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词。他重复这个操作直到S中没有列表里的单词为止。注意删除一个单词后可能会导致S中出现另一个列表中的单词
FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是互不相同的
请帮助FJ完成这些操作并输出最后的S
Input
The first line will contain S. The second line will contain N, the number of censored words. The next N lines contain the strings t_1 ... t_N. Each string will contain lower-case alphabet characters (in the range a..z), and the combined lengths of all these strings will be at most 10^5.
第一行包含一个字符串S
第二行包含一个整数N
接下来的N行,每行包含一个字符串,第i行的字符串是t_i
Output
The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
一行,输出操作后的S
Sample Input
begintheescapexecutionatthebreakofdawn
2
escape
execution
Sample Output
beginthatthebreakofdawn
AC自动机+栈,碰到结束节点弹栈即可
注意一下是用栈顶在trie树上向下扩张的
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
struct S1{
int trie[N+10][26],fail[N+10],End[N+10];
int root,tot;
void insert(char *s){
int len=strlen(s),p=root;
for (int i=0;i<len;i++){
if (!trie[p][s[i]-'a']) trie[p][s[i]-'a']=++tot;
p=trie[p][s[i]-'a'];
}
End[p]=len;
}
void make_fail(){
static int h[N+10];
int head=1,tail=0;
for (int i=0;i<26;i++) if (trie[root][i]) h[++tail]=trie[root][i];
for (;head<=tail;head++){
int Now=h[head];
for (int i=0;i<26;i++){
if (trie[Now][i]){
int son=trie[Now][i];
fail[son]=trie[fail[Now]][i];
h[++tail]=son;
}else trie[Now][i]=trie[fail[Now]][i];
}
}
}
void check(char *s){
static int stack[N+10];
static char Ans[N+10];
int len=strlen(s),top=0,p=root;
stack[top]=root;
for (int i=0;i<len;i++){
p=trie[stack[top]][s[i]-'a'];
stack[++top]=p,Ans[top]=s[i];
if (End[p]) top-=End[p];
}
for (int i=1;i<=top;i++) printf("%c",Ans[i]);putchar('\n');
}
}AC;//AC-automaton
char T[N+10];
int main(){
scanf("%s",T);
int n=read();
for (int i=1;i<=n;i++){
static char s[N+10];
scanf("%s",s);
AC.insert(s);
}
AC.make_fail();
AC.check(T);
return 0;
}
[Usaco2015 Feb]Censoring的更多相关文章
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- bzoj3940: [Usaco2015 Feb]Censoring
AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...
- 3942: [Usaco2015 Feb]Censoring
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...
- bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John has ...
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...
- BZOJ 3940: [Usaco2015 Feb]Censoring
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 173[Subm ...
- [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...
- Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)
3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...
- [Usaco2015 Feb]Censoring(bzoj 3942)
Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...
随机推荐
- error at ::0 can't find referenced pointcut pointCutName 错误解决方法
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: publi ...
- backtrace、backtrace_symbols、backtrace_symbols_fd-support for application self-debugging
backtrace是库函数引入的应用自调试函数. 系列里的三个函数可以缓冲或输出栈帧. #include <execinfo.h> int backtrace(void **buffer, ...
- Jmeter使用Http代理服务器报DNSName components must begin with a letter的错
最近了解到JMeter可以实现app的性能测试,需要借助JMeter的Http代理服务器来录制脚本. 于是,就按着网上的教程来进行操作,然而出师不利啊,刚启动就报错
- Hibernate quick start
Preface Working with both Object-Oriented software and Relational Databases can be cumbersome and ti ...
- IE8与vs2005冲突 添加MFC类向导错误解决方法—— internet explorer脚本错误
IE8 与 VS2005 冲突问题解决方法 问题表现为: MFC类向导添加类时,出现“当前页面的脚本发生错误”,进入MFC类向导后上方有一个小黄条“此网站的某个加载项运行失败.请检查"Int ...
- A JavaScript library for reading EXIF meta data from image files.
exif-js/exif-js: JavaScript library for reading EXIF image metadata https://github.com/exif-js/exif- ...
- Shell编程——Shell中的数学运算
在Linux Shell中进行数学运算,通常能够使用的运算符有: 简单运算: let [] (()) 高级运算: expr bc 1.let命令 let命令是bash内置命令.能够实现简单的算术以及逻 ...
- Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code
sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...
- JS判断按时间跳转到相应的页面
<!--时间段跳转js--><script language="javaScript" type="text/javascript"> ...
- 接口_简单get接口_第一个接口
import flask,json # print(__name__) ##__name__代表当前这个python文件 server = flask.Flask(__name__) #把咱们当前的这 ...