AC自动机

复习一下。。。 可惜又写错了

我们发现就是把单词建成ac自动机,然后把串在ac自动机上跑一遍,每到一个单词结束点就删除,删除是利用栈,每次弹出单词长度个字符就可以了

发现两个小问题,strlen很慢,不能写在循环里,danger必须在构造fail时全部传递好,否则在匹配时跑fail会达到n^2

至于这里danger为什么不转移,我也不是很清楚。。。大概是因为使用了trie图优化,并且不是统计单词出现次数,只是希望尽量找到靠前的单词删除

#include<bits/stdc++.h>
using namespace std;
const int N = ;
int n, top;
char s[N], t[N], st[N];
int mark[N], last[N];
struct AC {
int root, cnt;
int danger[N], child[N][], fail[N];
void insert()
{
int now = root, len = strlen(t);
for(int i = ; i < len; ++i)
{
int p = t[i] - 'a';
if(child[now][p] == ) child[now][p] = ++cnt;
now = child[now][p];
}
danger[now] = strlen(t);
}
void build_fail()
{
queue<int> q;
for(int i = ; i < ; ++i) if(child[root][i]) q.push(child[root][i]);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = ; i < ; ++i)
{
if(child[u][i] == ) child[u][i] = child[fail[u]][i];
else
{
fail[child[u][i]] = child[fail[u]][i];
q.push(child[u][i]);
}
}
}
}
void put_string()
{
int now = root, len = strlen(s);
for(int i = ; i < len; ++i)
{
st[++top] = s[i];
now = child[now][s[i] - 'a'];
last[top] = now;
top -= danger[now];
now = last[top];
}
}
} ac;
int main()
{
scanf("%s%d", s, &n);
for(int i = ; i <= n; ++i)
{
scanf("%s", t);
ac.insert();
}
ac.build_fail();
ac.put_string();
int cnt = ;
for(int i = ; i <= top; ++i) printf("%c", st[i]);
return ;
}

bzoj3940的更多相关文章

  1. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  2. bzoj3940: [Usaco2015 Feb]Censoring

    AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...

  3. BZOJ3940:[USACO]Censoring(AC自动机,栈)

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  4. bzoj3940&&bzoj3942 Ac自动机||kpm算法

    方法就是维护一个动态栈 记录栈的每一位匹配到串的哪一位的编号 第一道kmp第二道ac自动机 自己理会 #include<cstdio> #include<cstring> #i ...

  5. 【bzoj3940】[Usaco2015 Feb]Censoring

    [题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词.  FJ每次 ...

  6. 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

  7. 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)

    点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...

  8. BZOJ-3940:Censoring(AC自动机裸题)

    Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have p ...

  9. BZOJ3940: [Usaco2015 Feb]Censoring (AC自动机)

    题意:在文本串上删除一些字符串 每次优先删除从左边开始第一个满足的 删除后剩下的串连在一起重复删除步骤 直到不能删 题解:建fail 用栈存当前放进了那些字符 如果可以删 fail指针跳到前面去 好菜 ...

  10. [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)

    题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...

随机推荐

  1. 02Oracle Database 安装,卸载,系统服务,系统组件及系统表空间

    Oracle Database 安装,卸载,系统服务,系统组件及系统表空间 Oracle Database 安装 Oracle Database 卸载 Oracle Database 系统服务 Ora ...

  2. 2n皇后 - 回溯

    题目地址:http://www.51cpc.com/web/problem.php?id=1172 Summarize: 1. 递归回溯: 2. 先扫完一种皇后,再扫描另一种: 3. 循环输入: 4. ...

  3. linux常用操作记录

    vim:多行注释 vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率.   多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区 ...

  4. jsp页面应用简记

    1.记录总数据条数 <div class="" > <span class="">共有数据:<strong>${fn:len ...

  5. Flask上下文流程图

    如图:

  6. PYGAME学习笔记_01

    01_使用PYGAME创建图形窗口 1.1_游戏的初始化和退出 pygame.init() 写入并初始化所有PYGAME模块,使用其他模块之前,必须先调用init方法 pygame.quit() 卸载 ...

  7. 2.js原型的基本概念

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 腾讯云:iptables基础

    iptables 基础 iptables 基本命令 任务时间:5min ~ 10min iptables 可以简单理解为 Linux 系统内核级防火墙 netfilter 的用户态客户端. Linux ...

  9. UVA 10692 Huge Mod

    Problem X Huge Mod Input: standard input Output: standard output Time Limit: 1 second The operator f ...

  10. SGU - 321 - The Spy Network

    先上题目: 321. The Spy Network Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...