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 ;
}

最新文章

  1. 最简单jquery轮播图效果
  2. USB HID介绍【转】
  3. Python 结巴分词(1)分词
  4. 利用DataTable快速批量导数据
  5. JS组件Bootstrap实现弹出框和提示框效果代码
  6. iOS新特性引导页
  7. python 操作sqlite数据库
  8. android 对象传输及parcel机制
  9. c#基础: NetWorkStream类的主要属性
  10. $_FILES详解
  11. Jsp运行环境——Tomcat
  12. python时间序列分析
  13. iOS9中关于地址簿ABAddressBookXXX之类方法被废弃的解决
  14. 【图解】Hive文件存储格式
  15. Python——Django-应用的models.py内容
  16. MQTT服务器本地搭建
  17. DevExpress WinForms使用教程:SVG图库和Image Picker
  18. 泛泰A860(高通8064 cpu 1080p) 刷4.4专用中文recovery TWRP2.7.1.2版(三版通刷)
  19. HDU 2242 考研路茫茫——空调教室(边双连通分量+树形dp+重边标号)
  20. 术语-服务:PaaS

热门文章

  1. Vue.js 安装
  2. putchar()和getchar()使用解析
  3. Find The Multiple POJ - 1426 (BFS)
  4. TestNG超时测试
  5. 08 Python基础数据结构
  6. [bzoj4027][HEOI2015][兔子与樱花] (树形dp思想+玄学贪心)
  7. nyoj 911 Registration system(map)
  8. vue.js编程式路由导航 --- 由浅入深
  9. [K/3Cloud]关于数据库sa密码更改,管理中心登录不上的问题。
  10. Spring的发展【一】