zoukankan      html  css  js  c++  java
  • HDU5880 Family View ac自动机第二题

    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."

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

    The last line contains a string T (|T|1000000)T (|T|≤1000000).
    OutputFor 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.


    AC自动机+sum数组扫描记录“*段”

    
    
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <queue>
    #include <cstring>
    using namespace std;
    const int maxn=1000010;
    int Next[maxn][26],End[maxn],fail[maxn],h[maxn];
    int cnt,root=1,ans;
    int q[10000000],tail,head;
    int a[maxn],sum[maxn];
    char c[maxn] ;
    void _init()
    {
        //不要memset只要200ms 
        // memset(a,0,sizeof(a));
         ans=0;cnt=1;fail[root]=-1;//root的fail不能等于本身,不然不能重新开始。 
         head=tail=0;End[root]=0;
         for(int i=0;i<26;i++) Next[root][i]=0;
    }
    void _insert(char s[])
    {
        int L=strlen(s);
        int Now=root;
        for(int i=0;i<L;i++){
            if(!Next[Now][s[i]-'a']) { 
               Next[Now][s[i]-'a']=++cnt;
               fail[cnt]=0;
               End[cnt]=0;
               for(int i=0;i<26;i++) Next[cnt][i]=0;
               h[cnt]=h[Now]+1;
               a[cnt]=0;
            }
            Now=Next[Now][s[i]-'a'];
        }
        End[Now]++;
    }
    void _build()//bfs 
    {
        q[++head]=root;
        while(tail<head){
            int Now=q[++tail];
            for(int i=0;i<26;i++){
                if(Next[Now][i]){
                    if(Now==root) fail[Next[Now][i]]=root;
                    else{
                        int p=fail[Now];
                        while(p){//给儿子们找对象 
                            if(Next[p][i]){
                                fail[Next[Now][i]]=Next[p][i];
                                break;//找到了就停止 
                            }
                            p=fail[p];//配对 
                        }
                        if(!p) {
                           fail[Next[Now][i]]=root;//重新开始 
                        }
                    }
                    q[++head]=Next[Now][i];
                }
            }    
        }
    }
    void _query()
    {
        int L=strlen(c);
        int Now=root;
        for(int i=0;i<L;i++){
              int x=-1;
              if(c[i]>='A'&&c[i]<='Z') x=c[i]-'A';
              if(c[i]>='a'&&c[i]<='z') x=c[i]-'a';
              if(x==-1) {
                    Now=root;
                    continue;
              }
              while(Now!=root&&!Next[Now][x]) Now=fail[Now];
              Now=Next[Now][x];
              if(!Now)  Now=root;//即使失败,也不气馁,一切从零开始 
              int tmp=Now;
              while(tmp!=root){
                    if(End[tmp]>0){
                        a[i-h[tmp]+1]-=1;
                        a[i+1]+=1;
                        break;
                    }
                    tmp=fail[tmp];
              }
        }
        sum[0]=a[0];a[0]=0;
        for(int i=1;i<L;i++){
            sum[i]=sum[i-1]+a[i];
            a[i]=0;
        }
        for(int i=0;i<L;i++) {
           if(sum[i]<0) putchar('*');
           else putchar(c[i]);
        }
        putchar('
    ');
    }
    int main()
    {
        char s[maxn];
        int n,j,i,T;
        scanf("%d",&T);
        while(T--){
            
            _init();
            scanf("%d",&n);
            for(i=1;i<=n;i++){
               scanf("%s",s);
               _insert(s);//单词 
            }
            s[0]=getchar();
            gets(c);
            
            _build();
            _query();//文章 
        } 
        return 0;
    }
    View Code
    
    
    
     




  • 相关阅读:
    0317复利计算的回顾与总结
    0518 Scrum 项目 5.0
    0517 Scrum 项目4.0
    0512 Scrum 项目3.0
    实验三 进程调度模拟程序
    0505 Scrum 项目1.0
    0502团队项目 SCRUM团队成立
    0428 团队项目2.0
    0422团队项目
    实验二 作业调度模拟程序
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7727067.html
Copyright © 2011-2022 走看看