[USACO15FEB] 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.

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.

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

Sample Input

begintheescapexecutionatthebreakofdawn
2
escape
execution 

Sample Output

beginthatthebreakofdawn 

damn it!以后没事别逞强写指针,除非空间限制卡得特别厉害

可以记录每个字母在ac自动机上的位置,用栈维护当前句子

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<queue>
 5 #include<iostream>
 6 using namespace std;
 7 char s[100010],t[100010];
 8 struct node{
 9     int cnt;
10     struct node *fail,*nxt[26];
11     node(){
12         cnt=0;
13         fail=NULL;
14         memset(nxt,0,sizeof(nxt));
15     }
16 }*h,*p,*q,*sign[100010],*vis[200010][30];
17 int n,top,stk[100010],len[100010];
18 void insert(int x){
19     p=h;
20     for(int i=0,i_end=strlen(t);i<i_end;++i){
21         if(p->nxt[t[i]-‘a‘]==NULL)p->nxt[t[i]-‘a‘]=new node;
22         p=p->nxt[t[i]-‘a‘];
23     }
24     p->cnt=strlen(t);
25 }
26 void Getf(){
27     queue<node*> q;
28     q.push(h);
29     while(!q.empty()){
30         node *now=q.front();q.pop();
31         for(int i=0;i<26;++i)if(now->nxt[i]!=NULL){
32             if(now==h)now->nxt[i]->fail=h;
33             else{
34                 for(p=now->fail;p!=NULL;p=p->fail)if(p->nxt[i]!=NULL){
35                     now->nxt[i]->fail=p->nxt[i];
36                     break;
37                 }
38                 if(p==NULL)now->nxt[i]->fail=h;
39             }
40             q.push(now->nxt[i]);
41         }
42     }
43 }
44
45 node *F(node *now,int loc){
46     int x=s[loc]-‘a‘;
47     if(stk[top-1]>=0&&vis[stk[top-1]][x]!=NULL)return vis[stk[top-1]][x];
48     while(now->nxt[x]==NULL&&now!=h)now=now->fail;
49     now=now->nxt[x];
50     if(now==NULL)now=h;
51     if(stk[top-1]>=0)vis[stk[top-1]][x]=now;
52     return now;
53 }
54 void solve(){
55     sign[stk[0]=100005]=h;
56     for(int i=0,i_end=strlen(s);i<i_end;++i){
57         stk[++top]=i;
58         sign[i]=F(sign[stk[top-1]],i);
59         if(sign[i]->cnt)
60             top-=sign[i]->cnt;
61     }
62 }
63 int main(){
64     scanf("%s",s);
65     scanf("%d",&n);
66     h=new node;
67     for(int i=1;i<=n;++i)
68         scanf("%s",t),insert(i);
69     Getf();
70     solve();
71     for(int i=1;i<=top;++i)
72         printf("%c",s[stk[i]]);
73     return 0;
74 }
时间: 2024-10-21 01:10:21

[USACO15FEB] Censoring的相关文章

3940. [USACO15FEB]Censoring【AC自动机+栈】

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 inap

P4824 [USACO15FEB]Censoring (Silver) 审查(银)

传送门 一个串的匹配肯定考虑KMP 那就暴力KMP 记录一下到每个字符时匹配的位置 找到一个符合的串就标记然后暴力回跳 感觉好像太暴力了... #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=1e6+7; char ch[N],a[N]; int f[

关于KMP的一点思考

关于KMP的一点思考 KMP的\(next\)数组的性质很精妙,有必要开一个坑学习一下 Part 1 啥是next \(next[i]\)表示对于\(pre_i\)这个字符串,这个抠出来的字符串本身后缀和前缀相等的最长长度.是一个自变量只和这个子串有关的函数.这点很重要 由于保证了是最长长度,这个数有一些优良的性质,常常在关于一个串的循环表示或者周期表示中发挥作用. 注意到这个\(next[i]\)虽然代表是这个最长长度,但是值得注意的是,由于字符串从1开始编号,所以这个值也是那个前缀的下标.

「USACO15FEB」Censoring (Silver) 审查(银) 解题报告

题面 就是让你--在字符串A中,如果字符串B是A的子串,那么就删除在A中第一个出现的B,然后拼接在一起,一直重复上述步骤直到B不再是A的子串 |A|\(\le 10^6\) 思路: KMP+栈 1.由于是两个字符串匹配的问题,当然一下子就会想到KMP 2.由于是删去一段区间,很多人第一反应会想到链表,但是在这里,其实删除了一段后,对之前是没有影响的,并且,一定是从后往前删除,所以,更优的存储结构应该是栈. 3.有人会问,为什么删去对前面没有影响,这就根据KMP的原理,做到i这个位置的结果就是最优

洛谷 P3121 [USACO15FEB]审查(黄金)Censoring (Gold) 【AC自动机+栈】

这个和bzoj同名题不一样,有多个匹配串 但是思路是一样的,写个AC自动机,同样是开两个栈,一个存字符,一个存当前点在trie树上的位置,然后如果到了某个匹配串的末尾,则弹栈 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=100005; int n,t[N],top; char a[N],b[N],s

LuoguP3121 [USACO15FEB]审查(黄金)Censoring (Gold)【Hash做法】By cellur925

题目传送门 其实这题正解是AC自动机的,字符串哈希吸氧才能过的,但是我太菜了不会...只能先用哈希苟了. 在扫描单词的时候首先把各个单词的哈希值和长度存起来.然后按照长度从小到大将各单词排序.而那个长长的字符串呢,我们就把它一点一点往栈里塞,够最小长度单词的长度时,我们就比较下,这样反复下去.如果遇到相同的字符串,就把他们弹出. 这个思路最巧妙的一点感觉就是用栈了.我自己写哈希的时候遇到删除的情况就布吉岛怎么搞了qwq. 这里的哈希值不能预处理出来的,而是动态维护的.因为有可能会删掉子串.所以只

P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

吐槽 数据太水了吧,我AC自动机的trie建错了结果只是RE了两个点,还以为数组开小了改了好久 思路 看到多模板串,字符串匹配,且模板串总长度不长,就想到AC自动机 然后用栈维护当前的字符串位置,如果匹配到了,就从栈里逐个弹出对应的字符,并且回溯到匹配这个单词之前的节点 s每个字符最多会被出栈和入栈各两次,复杂度是\(O(n+m)\)的 代码 #include <cstdio> #include <algorithm> #include <cstring> #inclu

luogu_P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

栈模拟,哈希 #include<iostream> #include<cstdio> #define ri register int #define u unsigned long long namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&am

Censoring(bzoj 3940)

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 inap