【Trie图】BZOJ3940-[Usaco2015 Feb]Censoring
【题目大意】
有一个匹配串和多个模式串,现在不断删去匹配串中的模式串,求出最后匹配串剩下的部分。
【思路】
众所周知,KMP的题往往对应着一道AC自动机quq。本题同BZOJ3942(KMP),这里改成AC自动机即可。
我一开始写了原始的AC自动机,写挂了。后来思考了一下,应当用Trie图,机智地1A。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=+;
char str[MAXN];
int n,cnt;
struct ACauto
{
ACauto* next[];
ACauto* fail;
int id;
int sign;
ACauto()
{
for (int i=;i<;i++) next[i]=NULL;
fail=NULL;
id=++cnt;
sign=;
}
}; ACauto* rt=new ACauto(); void insert(char* s,ACauto* rt)
{
ACauto* tmp=rt;
for (int i=;s[i];i++)
{
int index=s[i]-'a';
if (tmp->next[index]==NULL)
tmp->next[index]=new ACauto();
tmp=tmp->next[index];
}
tmp->sign=strlen(s);
} void buildfail(ACauto* rt)//这里我们建立Trie图而不是Trie树的AC自动机
{
queue<ACauto*> que;
que.push(rt);
while (!que.empty())
{
ACauto* head=que.front();que.pop();
for (int i=;i<;i++)
{
if (head->next[i]==NULL)
{
if (head==rt) head->next[i]=rt;
else head->next[i]=head->fail->next[i];
}
else
{
if (head==rt) head->next[i]->fail=rt;
else
{
head->next[i]->fail=head->fail->next[i];
//if (head->next[i]->fail->sign) head->next[i]->sign=head->next[i]->fail->sign;/*注意!*/
}
que.push(head->next[i]);
}
}
}
} void init()
{
cnt=;
scanf("%s",str);
scanf("%d",&n);
for (int i=;i<n;i++)
{
char s[MAXN];
scanf("%s",s);
insert(s,rt);
}
buildfail(rt);
} void solve()
{
ACauto* a[MAXN];
char stack[MAXN];
int top=;
a[top]=rt;
for (int i=;str[i];i++)
{
ACauto* j=a[top];
stack[++top]=str[i];
int index=str[i]-'a';
a[top]=j->next[index];
if (a[top]->sign) top-=a[top]->sign;
}
stack[top+]='\0';
puts(stack+);
} int main()
{
init();
solve();
return ;
}
【Trie图】BZOJ3940-[Usaco2015 Feb]Censoring的更多相关文章
- [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...
- bzoj3940: [Usaco2015 Feb]Censoring
AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...
- BZOJ3940: [Usaco2015 Feb]Censoring (AC自动机)
题意:在文本串上删除一些字符串 每次优先删除从左边开始第一个满足的 删除后剩下的串连在一起重复删除步骤 直到不能删 题解:建fail 用栈存当前放进了那些字符 如果可以删 fail指针跳到前面去 好菜 ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...
- BZOJ 3940: [Usaco2015 Feb]Censoring
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 173[Subm ...
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- 3942: [Usaco2015 Feb]Censoring
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...
- bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John has ...
随机推荐
- 【leetcode 简单】第十二题 报数
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1&quo ...
- D - Binary Lexicographic Sequence URAL - 1081 (贪心)
题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...
- NASA: A Closer View of the Moon(近距离观察月球)
Posted to Twitter by @Astro_Alex, European Space Agency astronaut Alexander Gerst, this image shows ...
- ubuntu之一些安装配置的坑
前言 本博客记录自己使用ubuntu的一些错误和坑. ubuntu不支持yum下载安装机制 命令 sudo apt install yum 是可以安装yum的,但安装好后执行: $ yum insta ...
- django【ORM】model字段类型
1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntegerField 64位整数, -922 ...
- java 1.8 新特性 stream
并发提升 java 中Stream类似于hadoop中的数据分析的思路,只不过hadoop大,用的是多台机算机的计算生态,而java stream使用的单台计算机中的多cpu分析一块数据的过程.通过 ...
- 在ubuntu 上安装pycharm
1.首先在官网下载pycharm并进行提取,将提取的文件夹放在/usr下面(或者任意位置) 2.然后vi /etc/hosts 编辑 将0.0.0.0 account.jetbrains.com添加到 ...
- Mac——mac安装软件
命令行: perl: curl -L http://xrl.us/installperlosx | bash 参考资料: https://blog.csdn.net/yuxin6866/article ...
- scikit-learn模块学习笔记(数据预处理模块preprocessing)
本篇文章主要简单介绍sklearn中的数据预处理preprocessing模块,它可以对数据进行标准化.preprocessing 模块提供了数据预处理函数和预处理类,预处理类主要是为了方便添加到pi ...
- apache 软件历史版本查询
http://archive.apache.org/dist/ http://archive.apache.org/dist/struts/binaries/