【BZOJ3940】【Usaco2015 Feb】Censoring AC自动机

链接:

#include <stdio.h>
int main()
{
    puts("转载请注明出处[vmurder]谢谢");
    puts("网址:blog.csdn.net/vmurder/article/details/44960463");
}

题意:

题意同BZOJ3942,不过要删除的串是多串

http://blog.csdn.net/vmurder/article/details/44959895

题解:

……思路一模一样,除了不用kmp用AC自动机

代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 101000
#define T 26
#define inf 0x3f3f3f3f
using namespace std;
char s[N];
struct Eli
{
    int son[N][T],len[N],cnt;
    char s[N];
    void insert()
    {
        scanf("%s",s);
        int i,x=0,alp;
        for(i=0;s[i];i++)
        {
            alp=s[i]-‘a‘;
            if(!son[x][alp])son[x][alp]=++cnt;
            x=son[x][alp];
        }
        len[x]=max(len[x],i);
    }
    int fail[N];
    void keep()
    {
        queue<int>q;
        q.push(0);
        int i,u,v;
        while(!q.empty())
        {
            u=q.front(),q.pop();
            len[u]=max(len[u],len[fail[u]]);
            for(i=0;i<T;i++)
            {
                if(v=son[u][i])
                {
                    if(!u)fail[v]=0;
                    else fail[v]=son[fail[u]][i];
                    q.push(v);
                }
                else son[u][i]=son[fail[u]][i];
            }
        }
    }
}eli;
int f[N],stk[N],top;
int main()
{
    freopen("test.in","r",stdin);

    int i,n;

    scanf("%s%d",s+1,&n);
    while(n--)eli.insert();
    eli.keep();
    for(i=1;s[i];i++)
    {
        f[i]=eli.son[f[stk[top]]][s[i]-‘a‘];
        stk[++top]=i;
        top-=eli.len[f[i]];
    }
    for(i=1;i<=top;i++)printf("%c",s[stk[i]]);

    return 0;
}
时间: 2024-10-07 19:36:16

【BZOJ3940】【Usaco2015 Feb】Censoring AC自动机的相关文章

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB 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

Bzoj3940 [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 391  Solved: 183 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

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset

BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 404  Solved: 221[Submit][Status][Discuss] Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material

【bzoj3940】[Usaco2015 Feb]Censoring

[题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词. FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词.他重复这个操作直到S中 没有列表里的单词为止.注意删除一个单词后可能会导致S中出现另一个列表中的单词 FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是

[Usaco2015 Feb]Censoring

A. Censoring 题目描述 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 rathe

CENSORING——AC 自动机

题目 [题目描述] FJ 为它的奶牛订阅了很多杂志,balabala.......,其中有一些奶牛不宜的东西 (比如如何煮牛排). FJ 将杂志中所有的文章提取出来组成一个长度最多为 $ 10^5 $ 的字符串 S.他有一个要从 S 中删除的词语的列表,$ t_1,t_2...t_n $. FJ 每次找到最早的出现在列表里的子串,然后将其删去.他重复此过程,直到找不到这样的子串.值得注意的是删除一个单词可能产生一个新的之前并没有出现过的要被删除的单词. FJ 保证列表中没有一个字符串是另一个字符

BZOJ 3942 Usaco2015 Feb Censoring KMP算法

题目大意:给定两个串A和B,要求将A中删掉所有的B后输出 为何BC群刚有人问完我这题的[C++语法基础题]版之后就出了个KMP版的= = 维护一个栈,将A中的字符依次加进去,一旦A的栈顶出现了B就弹栈 用KMP算法来加速这个过程即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 using namespace st

bzoj3940 censoring 题解(AC自动机)

题目描述 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 inappropria