题目描述
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
输入输出格式
输入格式:
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.
输出格式:
The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
输入输出样例
输入样例#1: 复制begintheescapexecutionatthebreakofdawn 2 escape execution输出样例#1: 复制
beginthatthebreakofdawn
本题一看一个模式串和很多的单词匹配,很容易就像到ac机,然后在考虑怎么删除单词,暴力删除肯定是不行的,但是可以像链表一样,
开一个nxt 和pre 表示这个字符之后和之前的字符是哪个,通过改变这两个来完成删除操作
如何找到了一个单词,就把now 和 i全 设为0 ,这样也可以,但是会T
所以在保存一下到了字符串的i位置时,now在树上的位置,就行了。
1 #include"bits/stdc++.h" 2 using namespace std; 3 4 string s; 5 6 int tot; int head; 7 int tr[1600000][26]; 8 int lenn[2000000]; 9 int nxt[2000000]; 10 int pre[2000000]; 11 int pos[200000]; 12 int fail[2000000]; 13 int maxx=0; 14 15 inline void insert(string x) 16 { 17 int len=x.length(); int c=0; maxx=max(maxx,len); 18 for (int i=0;i<len;i++) 19 { 20 if(!tr[c][x[i]-'a'])tr[c][x[i]-'a']=++tot; 21 c=tr[c][x[i]-'a']; 22 } 23 lenn[c]=len; 24 // cout<<"insert: "<<c<<endl; 25 26 } 27 28 void Fail() 29 { 30 queue<int >q; for (int i=0;i<26;i++)if(tr[0][i])q.push(tr[0][i]); 31 while (!q.empty()) 32 { 33 int t=q.front();q.pop(); 34 for (int i=0;i<26;i++) 35 { 36 if(!tr[t][i])tr[t][i]=tr[fail[t]][i]; 37 else fail[tr[t][i]]=tr[fail[t]][i],q.push(tr[t][i]); 38 } 39 } 40 } 41 42 43 void work() 44 { 45 int now=0;vector<int > v; 46 for (int i=0;i<1e9;) 47 { 48 pos[i]=now=tr[now][s[i]-'a']; 49 50 if(lenn[now]) 51 { 52 int t=i; 53 for (int i=1;i<=lenn[now];i++)t=pre[t]; 54 pre[i+1]=t; 55 if(i+1>=s.length())nxt[t]=1e9; 56 else nxt[t]=i+1; 57 now=pos[t]; 58 if(t!=head)i=nxt[t]; //这里一定是nxt t,因为now等于这时 时,i应该到了下一个。 59 else i=nxt[head]; 60 /* for(int i=nxt[head];i!=1e9;i=nxt[i]) 61 cout<<s[i];cout<<endl;*/ 62 63 }else i=nxt[i]; 64 } 65 66 67 } 68 69 70 int main() 71 { 72 //freopen("testdataa.in","r",stdin); 73 //puts("HERE"); 74 cin>>s; int n;cin>>n; head=s.length()+1000; 75 // cout<<s<<endl; 76 for (int i=0;i<s.length()-1;i++)nxt[i]=i+1,pre[i+1]=i; 77 pre[s.length()-1]=s.length()-2; 78 pre[0]=head; 79 pre[head]=head; 80 nxt[s.length()-1]=1e9; 81 /* for(int i=0;i<1e9;i=nxt[i]) 82 cout<<i<<" "<<s[i]<<endl; 83 */ 84 // puts("_____"); 85 for (int i=1;i<=n;i++) 86 { 87 string x;cin>>x; insert(x); 88 } 89 Fail(); 90 // puts("here"); 91 work(); 92 //// cout<<"MAXX: "<<maxx<<endl;0 93 //for (int i=0;i<s.length();i++)cout<<i<<" "<<nxt[i]<<endl; 94 95 for(int i=nxt[head];i!=1e9;i=nxt[i]) 96 cout<<s[i];cout<<endl; 97 // cout<<s.length()<<endl; 98 }