ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P3121 [USACO15FEB]审查(黄金)Censoring (Gold)(ac自动机)

2019-03-31 09:52:07  阅读:278  来源: 互联网

标签:nxt ac word Gold int Censoring 单词 FJ censored


题目描述

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 }

 















标签:nxt,ac,word,Gold,int,Censoring,单词,FJ,censored
来源: https://www.cnblogs.com/zhangbuang/p/10630282.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有