zoukankan      html  css  js  c++  java
  • BZOJ3940: [Usaco2015 Feb]Censoring

    BZOJ3940: [Usaco2015 Feb]Censoring

    Description

    Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty 
    of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest 
    issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his 
    cows not see (clearly, the magazine is in need of better editorial oversight).
    FJ has taken all of the text from the magazine to create the string S of length at most 10^5 characters. 
    He has a list of censored words t_1 ... t_N that he wishes to delete from S. To do so Farmer John finds 
    the earliest occurrence of a censored word in S (having the earliest start index) and removes that instance 
    of the word from S. He then repeats the process again, deleting the earliest occurrence of a censored word 
    from S, repeating until there are no more occurrences of censored words in S. Note that the deletion of one 
    censored word might create a new occurrence of a censored word that didn't exist before.
    Farmer John notes that the censored words have the property that no censored word appears as a substring of 
    another censored word. In particular this means the censored word with earliest index in S is uniquely 
    defined.Please help FJ determine the final contents of S after censoring is complete.
    FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S。他有一个包含n个单词的列表,列表里的n个单词
    记为t_1...t_N。他希望从S中删除这些单词。 
    FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词。他重复这个操作直到S中
    没有列表里的单词为止。注意删除一个单词后可能会导致S中出现另一个列表中的单词 
    FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是互不相同的 
    请帮助FJ完成这些操作并输出最后的S

    Input

    The first line will contain S. The second line will contain N, the number of censored words. The next N lines contain the strings t_1 ... t_N. Each string will contain lower-case alphabet characters (in the range a..z), and the combined lengths of all these strings will be at most 10^5.
    第一行包含一个字符串S 
    第二行包含一个整数N 
    接下来的N行,每行包含一个字符串,第i行的字符串是t_i

    Output

    The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
    一行,输出操作后的S

    Sample Input

    begintheescapexecutionatthebreakofdawn
    2
    escape
    execution

    Sample Output

    beginthatthebreakofdawn

    题解Here!
    多模式串匹配,不是$AC$自动机,就是$SA/SAM$。
    这个题$AC$自动机就好了。
    开两个栈,一个记录当然栈内合法的字符,一个记录当前节点扫到AC自动机的哪个地方了。
    每当发现一个能删去的字符串的时候,只需要把两个栈都减去字符串长度即可,把u修改一下,继续往后扫即可,最后输出第一个栈即可。
    附代码:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #define MAXN 100010
    using namespace std;
    int n,size=0,top=0;
    int stack_one[MAXN],stack_two[MAXN];
    char str[MAXN],ch[MAXN];
    struct AC{
        int fail,val,son[26];
        AC(){
            fail=val=0;
            memset(son,0,sizeof(son));
        }
    }a[MAXN];
    inline int read(){
    	int date=0,w=1;char c=0;
    	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
    	return date*w;
    }
    inline int idx(char x){return x-'a';}
    void insert(char *s){
        int u=0,l=strlen(s);
        for(int i=0;i<l;i++){
            int c=idx(s[i]);
            if(!a[u].son[c])a[u].son[c]=++size;
            u=a[u].son[c];
        }
        a[u].val=l;
    }
    void buildtree(){
        int u,v;
        queue<int> q;
        for(int i=0;i<26;i++)
        if(a[0].son[i]){
            a[a[0].son[i]].fail=0;
            q.push(a[0].son[i]);
        }
        while(!q.empty()){
            u=q.front();
            q.pop();
            for(int i=0;i<26;i++){
                if(a[u].son[i]){
                    a[a[u].son[i]].fail=a[a[u].fail].son[i];
                    q.push(a[u].son[i]);
                }
                else a[u].son[i]=a[a[u].fail].son[i];
            }
        }
    }
    void query(char *s){
        int u=0,l=strlen(s);
        for(int i=0;i<l;i++){
            int c=idx(s[i]);
            u=a[u].son[c];
            stack_one[++top]=i;
            stack_two[top]=u;
            if(a[u].val){
                top-=a[u].val;
                if(!top)u=0;
                else u=stack_two[top];
            }
        }
    }
    void work(){
        query(str);
        for(int i=1;i<=top;i++)printf("%c",str[stack_one[i]]);
    }
    void init(){
        scanf("%s",str);
        n=read();
        for(int i=1;i<=n;i++){
            scanf("%s",ch);
            insert(ch);
        }
        buildtree();
    }
    int main(){
        init();
        work();
        return 0;
    }
    
  • 相关阅读:
    【Cocos2d-X游戏实战开发】捕鱼达人之开发前准备工作(一)
    NetBeans + Xdebug 调试WordPress
    【Cocos2d-X游戏实战开发】捕鱼达人之单例对象的设计(二)
    源代码静态分析工具
    Flash Builder 条件编译的实现
    Maven插件之portable-config-maven-plugin(不同环境打包)
    生成8位随机不重复的数字编号
    【剑指Offer学习】【面试题63:二叉搜索树的第k个结点】
    51nod 1413:权势二进制
    leetcode_Isomorphic Strings _easy
  • 原文地址:https://www.cnblogs.com/Yangrui-Blog/p/9427881.html
Copyright © 2011-2022 走看看