日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

[洛谷3121]审查

發(fā)布時間:2024/1/17 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [洛谷3121]审查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

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中找到最早出現(xiàn)的列表中的單詞(最早出現(xiàn)指該單詞的開始位置最小),然后從S中刪除這個單詞。他重復(fù)這個操作直到S中沒有列表里的單詞為止。注意刪除一個單詞后可能會導(dǎo)致S中出現(xiàn)另一個列表中的單詞

FJ注意到列表中的單詞不會出現(xiàn)一個單詞是另一個單詞子串的情況,這意味著每個列表中的單詞在S中出現(xiàn)的開始位置是互不相同的

請幫助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: 復(fù)制 begintheescapexecutionatthebreakofdawn 2 escape execution 輸出樣例#1: 復(fù)制 beginthatthebreakofdawn

AC自動機+棧。其實也很好想,就是把單詞放在AC自動機上跑,每刪除一段,就回到刪除的這一段前一個字母表示的節(jié)點上,這個用棧存一下就行了。
代碼
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #define M 500010 6 using namespace std; 7 int n,cnt,top; 8 int st1[M],st2[M]; 9 int val[M]; 10 int fail[M]; 11 int ch[M][27]; 12 char s[M],S[M]; 13 void insert(string s) 14 { 15 int len=s.length(); 16 int now=0; 17 for(int i=0;i<len;i++) 18 { 19 if(!ch[now][s[i]-'a']) ch[now][s[i]-'a']=++cnt; 20 now=ch[now][s[i]-'a']; 21 } 22 val[now]=len; 23 } 24 void build_fail() 25 { 26 queue<int>q; 27 for(int i=0;i<26;i++) 28 if(ch[0][i]) 29 q.push(ch[0][i]); 30 while(!q.empty()) 31 { 32 int u=q.front(); q.pop(); 33 for(int i=0;i<26;i++) 34 { 35 if(ch[u][i]) 36 { 37 fail[ch[u][i]]=ch[fail[u]][i]; 38 q.push(ch[u][i]); 39 } 40 else ch[u][i]=ch[fail[u]][i]; 41 } 42 } 43 } 44 void work(string s) 45 { 46 int len=s.length(),now=0; 47 for(int i=0;i<len;i++) 48 { 49 int v=s[i]-'a'; 50 now=ch[now][v]; 51 st1[++top]=i; 52 st2[top]=now; 53 if(val[now]!=0) 54 { 55 top-=val[now]; 56 if(top<=0) now=0; 57 else now=st2[top]; 58 } 59 } 60 } 61 int main() 62 { 63 scanf("%s",S); 64 scanf("%d",&n); 65 for(int i=1;i<=n;i++) 66 { 67 scanf("%s",s); 68 insert(s); 69 } 70 build_fail(); 71 work(S); 72 for(int i=1;i<=top;i++) printf("%c",S[st1[i]]); 73 return 0; 74 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/Slrslr/p/9496146.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的[洛谷3121]审查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。