HDU 5880 Family View (AC自动机)

Family View

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.
/*
 * HDU 5880 Family View
 * 把文本串中所有的模式串换为等长度的'*'
 * 
 * AC自动机
 * 将模式串建立AC自动机,然后进行文本串匹配,求出文本串的每个前缀
 * 所包含的最长后缀,并且是模式串,用pos数组记录位置,然后扫描一遍输出即可。
 */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 1000000+100;
char str[MAXN];
int pos[MAXN];
struct Aho_Corasick
{
    const static int maxnode = 1000000+100;
    const static int type = 26;
    int next[maxnode][type],fail[maxnode],end[maxnode],l[maxnode];
    int root,L;
    int newnode()
    {
        for(int i = 0;i<type;i++)
            next[L][i] = -1;
        l[L]=0;
        end[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char* str)
    {
        int len = strlen(str);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            int id=str[i]-'a';
            if(next[now][id] == -1)
                next[now][id] = newnode();
            now=next[now][id];
        }
        end[now]=1;
        l[now]=len;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < type;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < type;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    void work(char* str)
    {
        int id;
        int len = strlen(str);
        int now = root;
        memset(pos,0,sizeof(pos));
        for(int i = 0;i < len;i++)
        {
            if(str[i]>='A'&&str[i]<='Z') id=str[i]-'A';
            else if(str[i]>='a'&&str[i]<='z') id=str[i]-'a';
            else continue;
            now = next[now][id];
            int temp=now;
            while(temp != root)
            {
                if(end[temp] != -1)
                {
                    pos[i+1]-=1;
                    pos[i-l[temp]+1]+=1;
                    break;
                }
                temp = fail[temp];
            }
        }
        long long cnt=0;
        for(int i=0;i<len;i++)
        {
            cnt+=pos[i];
            if(cnt<=0) printf("%c",str[i]);
            else printf("*");
        }
        printf("\n");
    }
}AC;

int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        AC.init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            AC.insert(str);
        }
        AC.build();
        getchar();
        gets(str);
        AC.work(str);
    }
    return 0;
}

 

 

posted @ 2016-09-17 23:52  季末Despair  阅读(1068)  评论(0编辑  收藏  举报