hdu5880 Family View

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5880

题目:

Family View

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2109    Accepted Submission(s): 445


Problem Description
Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and social networking services. A family view can help you to prevent your children access to some content which are not suitable for them. 

Take an MMORPG game as an example, given a sentence T, and a list of forbidden words {P}, your job is to use '*' to subsititute all the characters, which is a part of the substring matched with at least one forbidden word in the list (case-insensitive).

For example, T is: "I love Beijing's Tiananmen, the sun rises over Tiananmen. Our great leader Chairman Mao, he leades us marching on."

And {P} is: {"tiananmen", "eat"}

The result should be: "I love Beijing's *********, the sun rises over *********. Our gr*** leader Chairman Mao, he leades us marching on."
 

 

Input
The first line contains the number of test cases. For each test case:
The first line contains an integer n, represneting the size of the forbidden words list P. Each line of the next n lines contains a forbidden words Pi (1|Pi|1000000,|Pi|1000000) where Pi only contains lowercase letters.

The last line contains a string T (|T|1000000).
 

 

Output
For each case output the sentence in a line.
 

 

Sample Input
1 3 trump ri o Donald John Trump (born June 14, 1946) is an American businessman, television personality, author, politician, and the Republican Party nominee for President of the United States in the 2016 election. He is chairman of The Trump Organization, which is the principal holding company for his real estate ventures and other business interests.
 

 

Sample Output
D*nald J*hn ***** (b*rn June 14, 1946) is an Ame**can businessman, televisi*n pers*nality, auth*r, p*litician, and the Republican Party n*minee f*r President *f the United States in the 2016 electi*n. He is chairman *f The ***** *rganizati*n, which is the p**ncipal h*lding c*mpany f*r his real estate ventures and *ther business interests.
 

 

Source

 

思路:

  裸ac自动机题,走到一个节点看下有匹配的没,有的话用最长串的长度把位置标记一下,然后输出即可。

  ac自动机中实际节点数并不需要开到27*1e6!!!数据并没那么大   

  只需开到27*1e5

  1 #include <queue>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5 
  6 struct AC_auto
  7 {
  8     const static int LetterSize = 27;
  9     const static int TrieSize = 27 * ( 1e5 + 50);
 10 
 11     int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize];
 12 
 13     int newnode(void)
 14     {
 15         memset(next[tot],-1,sizeof(next[tot]));
 16         end[tot] = 0;
 17         return tot++;
 18     }
 19 
 20     void init(void)
 21     {
 22         tot = 0;
 23         root = newnode();
 24     }
 25 
 26     int getidx(char x)
 27     {
 28         if(x<='z'&&x>='a') return x - 'a';
 29         if(x<='Z'&&x>='A') return x - 'A';
 30         return 26;
 31     }
 32 
 33     void insert(char *ss)
 34     {
 35         int len = strlen(ss);
 36         int now = root;
 37         for(int i = 0; i < len; i++)
 38         {
 39             int idx = getidx(ss[i]);
 40             if(next[now][idx] == -1)
 41                 next[now][idx] = newnode();
 42             now = next[now][idx];
 43         }
 44         end[now] = len;
 45     }
 46 
 47     void build(void)
 48     {
 49         queue<int>Q;
 50         fail[root] = root;
 51         for(int i = 0; i < LetterSize; i++)
 52             if(next[root][i] == -1)
 53                 next[root][i] = root;
 54             else
 55                 fail[next[root][i]] = root,Q.push(next[root][i]);
 56         while(Q.size())
 57         {
 58             int now = Q.front();Q.pop();
 59             for(int i = 0; i < LetterSize; i++)
 60             if(next[now][i] == -1)   next[now][i] = next[fail[now]][i];
 61             else
 62                 fail[next[now][i]] = next[fail[now]][i],Q.push(next[now][i]);
 63         }
 64     }
 65 
 66     void match(char *ss,int *cnt)
 67     {
 68         int len,now;
 69         len = strlen(ss),now = root;
 70         for(int i = 0; i < len; i++)
 71         {
 72             int idx = getidx(ss[i]);
 73             int tmp = now = next[now][idx], ret = 0;
 74             while(tmp)
 75             {
 76                 ret = max( ret, end[tmp]);
 77                 tmp = fail[tmp];
 78             }
 79             if(ret)
 80                 cnt[i-ret+1]++,cnt[i+1]--;
 81         }
 82     }
 83     void debug()
 84     {
 85         for(int i = 0;i < tot;i++)
 86         {
 87             printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
 88             for(int j = 0;j < LetterSize;j++)
 89                 printf("%3d",next[i][j]);
 90             printf("]\n");
 91         }
 92     }
 93 }ac;
 94 
 95 char ss[1000007];
 96 int cnt[1000007];
 97 
 98 int main(void)
 99 {
100     int t,n;
101     scanf("%d",&t);
102     while(t--)
103     {
104         ac.init();
105         scanf("%d",&n);
106         for(int i=1;i<=n;i++)   scanf("%s",ss),ac.insert(ss);
107         getchar(),gets(ss);
108         ac.build();
109         ac.match(ss,cnt);
110         for(int i=0,ret=0,len=strlen(ss);i<len;i++)
111         {
112             ret+=cnt[i],cnt[i]=0;
113             if(ret>0)   ss[i]='*';
114         }
115         printf("%s\n",ss);
116     }
117     return 0;
118 }

 

posted @ 2017-10-03 15:20  weeping  阅读(227)  评论(0编辑  收藏  举报