【题目大意】

有一个匹配串和多个模式串,现在不断删去匹配串中的模式串,求出最后匹配串剩下的部分。

【思路】

众所周知,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 ;
}

最新文章

  1. 我的前端故事----优美的编辑器GitHub Atom
  2. RecyclerView的下拉刷新和加载更多 动画
  3. 【PHP面向对象(OOP)编程入门教程】12.重载新的方法(parent::)
  4. CentOS 6.5下搭建LAMP环境详细步骤
  5. 34-Ajax辅助方法
  6. IT新人论成长
  7. [原]Unity3D深入浅出 - 粒子系统(Particle System)
  8. Android线控的使用
  9. BZOJ_1208_&amp;_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)
  10. hdu 3333 Turing Tree
  11. OSI模型第四层传输层--TCP协议
  12. Python 接口:从协议到抽象基类
  13. [转]Python in Visual Studio Code
  14. C#中string.Format 用法详解
  15. A.CTable 自动创建数据表
  16. 深入理解Plasma(二)Plasma 细节
  17. Spring之Bean的作用域与生命周期
  18. OSGeo.OGR.Geometry
  19. Web应用程序项目XXXX已配置为使用IIS。无法访问IIS 元数据库。您没有足够的特权访问计算机上的IIS
  20. MyBatis与JDBC连接数据库所使用的url之间的差异

热门文章

  1. NYOJ 925 国王的烦恼 (并查集)
  2. bzoj 2653 二分答案+可持久化线段树
  3. 安装 Google BBR 加速VPS网络
  4. 二叉树的层序遍历(levelordertraverse)
  5. PDFRender4NET的使用之pdf转图片
  6. 121.Best Time to Buy and Sell Stock---dp
  7. 对 makefile 中 .DEFAULT 的理解
  8. JSP基础与提高(一).md
  9. Linux软件管理器(如何使用软件管理器来管理软件)
  10. fedroa20 没法开启ntpd服务器