所学的AC自动机都源于斌哥和昀神的想法。

题意:求目标串中出现了几个模式串。

使用一个int型的end数组记录,查询一次。

#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int maxw = 50 * 10000 + 10;
const int sigma_size = 26;
const int maxl = 1000000 + 10; struct Trie{
int next[maxw][sigma_size],fail[maxw],end[maxw];
int root,L;
int newnode(){
for(int i=0;i<sigma_size;i++)
next[L][i]=-1;
end[L++]=0;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void insert(const char *buf){
int now=root,len=strlen(buf);
for(int i=0;i<len;i++){
if(next[now][buf[i]-'a']==-1)
next[now][buf[i]-'a']=newnode();
now=next[now][buf[i]-'a'];
}
end[now]++;
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0;i<sigma_size;i++)
if(next[root][i]==-1)
next[root][i]=root;
else{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now=Q.front();
Q.pop();
for(int i=0;i<sigma_size;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int query(const char *buf){
int now=root,len=strlen(buf);
int res=0;
for(int i=0;i<len;i++){
now=next[now][buf[i]-'a'];
int tmp=now;
while(tmp!=root){
res+=end[tmp];
end[tmp]=0;
tmp=fail[tmp];
}
}
return res;
}
}; Trie ac;
char buf[maxl]; int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ac.init();
for(int i=0;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
int ans=ac.query(buf);
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. 使用 dynamic 标记解析JSON字符串
  2. Android之网络请求库
  3. Oracle MySQL Server 安全漏洞
  4. careercup-排序和查找 11.3
  5. 理解Java的GC日志
  6. Directx11学习笔记【一】 最简单的windows程序HelloWin
  7. 网站优化记录-通过命令预编译Asp.net 网站,成功优化到毫秒级别。
  8. java连接VMware虚拟机Oracle数据库问题
  9. UI设计切忌墨守成规,但改变也须用数据说话
  10. 包建强的培训课程(6):Android App瘦身优化
  11. 6s ios9.0平台 微信小程序的fixed定位兼容性问题
  12. Codeforces Round #540 (Div. 3)
  13. 剑指Offer-第一个只出现一次的字符位置
  14. CentOS下安装Docker-CE
  15. 有关C#中List排序的总结
  16. python调用ansible接口API执行命令
  17. python第十四课--排序及自定义函数之案例一:选择排序
  18. DBGrid添加行号编写笔记
  19. IOS开发之----代码块的使用(二)
  20. 音频audio,加层父级

热门文章

  1. LOJ#2471「九省联考 2018」一双木棋 MinMax博弈+记搜
  2. 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford
  3. WordPress插件会员简化1.58 -任意文件下载漏洞(附poc)
  4. [转] 关于Struts-JSON配置(详解带实例struts2的json数据支持)
  5. URAL 1993 This cheeseburger you don&#39;t need 模拟题
  6. 创建Windows窗体 : WinMain() 与 WndProc()
  7. Spring MVC概述
  8. gdb 与函数过程
  9. visual studio 2008试用版的评估期(万能破解)
  10. 读CRecordset