zoukankan      html  css  js  c++  java
  • hdu 5880 AC自动机

    Family View

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


    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
    2016 ACM/ICPC Asia Regional Qingdao Online
    /*
    hdu 5880 AC自动机
    
    problem:
    给你一些子串,然后在文章中将这些子串屏蔽掉.
    
    solve:
    用AC自动机扫一扫,然后给需要屏蔽的地方打下标记
    
    hhh-2016-09-17 20:51:55
    */
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <queue>
    
    using namespace std;
    const int maxn = 1001000;
    
    char s[maxn];
    int dis[maxn];
    int ans[maxn];
    
    struct Tire
    {
        int nex[1001000][27],fail[1001000],ed[1001000];
        int root,L;
        int newnode()
        {
            for(int i = 0; i < 26; i++)
                nex[L][i] = -1;
            ed[L++] = 0;
            return L-1;
        }
    
        void ini()
        {
            L = 0,root = newnode();
        }
    
        int cha(char x)
        {
            return x-'a';
        }
    
        void inser(char buf[])
        {
            int len = strlen(buf);
            int now = root;
            for(int i = 0; i < len; i++)
            {
                int ta = cha(buf[i]);
                if(nex[now][ta] == -1)
                    nex[now][ta] = newnode();
                now = nex[now][ta];
            }
            ed[now] = 1;
            dis[now] = len;
        }
    
        void build()
        {
            queue<int >q;
            fail[root] = root;
            for(int i = 0; i < 26; i++)
                if(nex[root][i] == -1)
                    nex[root][i] = root;
                else
                {
                    fail[nex[root][i]] = root;
                    q.push(nex[root][i]);
                }
            while(!q.empty())
            {
                int now = q.front();
    //            if(ed[fail[now]])
    //                ed[now] = 1;
                q.pop();
                for(int i = 0; i < 26; i++)
                {
                    if(nex[now][i] == -1)
                        nex[now][i] = nex[fail[now]][i];
                    else
                    {
                        fail[nex[now][i]] = nex[fail[now]][i];
                        q.push(nex[now][i]);
                    }
                }
            }
        }
    
        void solve(char* str)
        {
            int cur = root;
            int len = strlen(str);
            int index;
            for(int i = 0; i < len; i++)
            {
                if(str[i]>='A'&&str[i]<='Z')
                {
                    index = str[i] - 'A';
                }
                else if(str[i]>='a'&&str[i]<='z')
                {
                    index = str[i] - 'a';
                }
                else{
                    cur = root;   //a,,,b,,,c,,,d
                    continue;
                }
                cur = nex[cur][index];
                int tp = cur;
    
                while(tp != root)
                {
                    if(ed[tp]){
                        ans[i+1] -= 1;
                        ans[i - dis[tp]+1] += 1;
                        break;
                    }
                    tp = fail[tp];
                }
            }
        }
    };
    
    Tire ac;
    
    int main()
    {
        int t;
        int n;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &n);
            ac.ini();
            for(int i=0; i<n; i++)
            {
                scanf("%s", s);
                ac.inser(s);
            }
            ac.build();
            getchar();
            gets(s);
    //        puts(s);
            memset(ans, 0, sizeof(ans));
            ac.solve(s);
            int len = strlen(s);
            long long int tans = 0;
    
            for(int i=0; i<len; i++)
            {
                tans += ans[i];
                if(tans <= 0) printf("%c", s[i]);
                else printf("*");
    
            }
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    numpy—————数组操作
    ML———聚类算法之K-Means
    DataFrame————数据离散化处理(元素定位与离散化处理)
    windows 搭建和配置 hadoop + 踩过的坑
    Pandas -----简述 Series和DataFrame
    numpy 函数和用法总结、示例
    分词————jieba分词(Python)
    (31)本地yum仓库的安装配置
    (30)zookeeper的数据结构
    (29)zookeeper的命令行客户端
  • 原文地址:https://www.cnblogs.com/Przz/p/5879671.html
Copyright © 2011-2022 走看看