Hdu 5880 Family View AC自动机 水

来源:互联网 发布:电脑实用软件推荐 编辑:程序博客网 时间:2024/06/04 20:07

Family View

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


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
13trumprioDonald 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
2016 ACM/ICPC Asia Regional Qingdao Online



给你一组串和一段话,要求把所有这段话当中出现匹配串的位置变为 * 


AC自动机。套个模板,over~


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=1000005,maxk=26,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int q[maxn],b[maxn];char t[maxn];int num;struct node{    int fail,next[maxk];     int cnt,len;    void init() {        fail=-1;        for (int i=0;i<maxk;i++) next[i]=-1;        cnt=len=0;    }};node a[maxn];void insert(int root,int len) {    int now=root;    int i;    for (i=0;i<len;i++) {        int pos=t[i]-'a';        if (a[now].next[pos]==-1) {            a[now].next[pos]=++num;            a[num].init();        }        now=a[now].next[pos];    }    a[now].cnt++;    a[now].len=max(a[now].len,len);}void buildfail(int root) {    int p=root;    int front=0,tail=0,i;    for (i=0;i<maxk;i++) {        if (a[p].next[i]!=-1) {            a[a[p].next[i]].fail=root;            q[tail++]=a[p].next[i];        } else a[p].next[i]=root;    }    while (front<tail) {        p=q[front];        for (i=0;i<maxk;i++) {            if (a[p].next[i]==-1)                 a[p].next[i]=a[a[p].fail].next[i];            else {                a[a[p].next[i]].fail=a[a[p].fail].next[i];                q[tail++]=a[p].next[i];            }        }        front++;    }}void search(string s,int root,int len) {      int k=0,i;      int p=root,now;      for (i=0;i<len;i++) {        if (s[i]>='a'&&s[i]<='z') p=a[p].next[s[i]-'a']; else {        p=root;continue;        }        now=p;          while (now!=root&&a[now].cnt!=-1) {              if (a[now].cnt) {                b[i-a[now].len+1]++;                b[i+1]--;            }            now=a[now].fail;          }    }}  int main() {    num=0;    string s,w;    int cas;    scanf("%d",&cas);    while (cas--) {        int n,i,j,len;        mem0(b);        scanf("%d",&n);        num=0;        a[0].init();        for (i=1;i<=n;i++) {            scanf("%s",t);            len=strlen(t);            insert(0,len);        }        getchar();        getline(cin,s);        len=s.length();w=s;        for (i=0;i<len;i++) {            if (s[i]>='A'&&s[i]<='Z') s[i]=(char)('a'-'A'+s[i]);        }        buildfail(0);        search(s,0,len);        int sum=0;        for (i=0;i<len;i++) {            sum+=b[i];            if (sum>0) printf("*"); else printf("%c",w[i]);        }        printf("\n");    }    return 0;}