[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,id;
10     struct node *fail,*nxt[26];
11     node(){
12         cnt=0;
13         id=0;
14         fail=NULL;
15         memset(nxt,0,sizeof(nxt));
16     }
17 }*h,*sign[100010],*vis[200010][30];
18 int n,top,chk,stk[100010],len[100010];
19 void insert(int x){
20     node *p=h;
21     for(int i=0,i_end=strlen(t);i<i_end;++i){
22         if(p->nxt[t[i]-'a']==NULL)p->nxt[t[i]-'a']=new node;
23         p->id=++chk;
24         p=p->nxt[t[i]-'a'];
25     }
26     p->id=++chk;
27     p->cnt=strlen(t);
28 }
29 void Getf(){
30     queue<node*> q;
31     for(int i=0;i<26;++i)
32         if(h->nxt[i]!=NULL)q.push(h->nxt[i]);
33     while(!q.empty()){
34         node *now=q.front();q.pop();
35         for(int i=0;i<26;++i){
36             if(now->fail==NULL)now->fail=h;
37             if(now->nxt[i]==NULL){
38                 now->nxt[i]=now->fail->nxt[i];
39                 continue;
40             }
41             now->nxt[i]->fail=now->fail->nxt[i];
42             if(now->nxt[i]!=h)q.push(now->nxt[i]);
43         }
44     }
45 }
46 void solve(){
47     node *p=h;
48     sign[0]=h;
49     for(int i=0,i_end=strlen(s);i<i_end;++i){
50         p=p->nxt[s[i]-'a'];
51         if(p==NULL)p=h;
52         sign[++top]=p;
53         stk[top]=i;
54         if(p->cnt>0){
55             top-=p->cnt;
56             p=sign[top];    
57         }
58     }
59 }
60 int main(){
61     scanf("%s",s);
62     scanf("%d",&n);
63     h=new node;
64     for(int i=1;i<=n;++i)
65         scanf("%s",t),insert(i);
66     Getf();
67     solve();
68     for(int i=1;i<=top;++i)
69         printf("%c",s[stk[i]]);
70     return 0;
71 }

 

posted @ 2017-06-11 23:24  人棍王楼下的AI  阅读(199)  评论(0编辑  收藏  举报