【模板】AC自动机(二次加强版)
2024-10-21 05:54:19
模板
\(Problem:\) 求 \(n\) 个模式串在文本串中出现的次数
\(templete:\) \(Luogu5357\)
\(Code\)
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2e5 + 5;
int tr[N][26] , sum[N * 26] , id[N * 26] , tag[N * 26] , fail[N * 26] , q[N * 26] , ans[N] , map[N];
int n , tot;
char s[2000005];
void insert(int x)
{
int len = strlen(s) , u = 0;
for(register int i = 0; i < len; i++)
{
int ch = s[i] - 'a';
if (!tr[u][ch]) tr[u][ch] = ++tot;
u = tr[u][ch];
}
if (!id[u]) id[u] = x;
map[x] = id[u];
}
void get_fail()
{
int h = 0 , t = 0;
for(register int i = 0; i < 26; i++) if (tr[0][i]) q[++t] = tr[0][i];
while (h < t)
{
int now = q[++h];
for(register int i = 0; i < 26; i++)
{
if (!tr[now][i]) {tr[now][i] = tr[fail[now]][i]; continue;}
fail[tr[now][i]] = tr[fail[now]][i];
++tag[tr[fail[now]][i]] , q[++t] = tr[now][i];
}
}
}
void calc()
{
int len = strlen(s) , u = 0;
for(register int i = 0; i < len; i++)
u = tr[u][s[i] - 'a'] , ++sum[u];
int h = 0 , t = 0;
for(register int i = 1; i <= tot; i++)
if (!tag[i]) ans[id[i]] = sum[i] , q[++t] = i;
while (h < t)
{
int now = q[++h] , k = fail[now];
--tag[k] , sum[k] += sum[now] , ans[id[k]] = sum[k];
if (!tag[k]) q[++t] = k;
}
}
int main()
{
scanf("%d" , &n);
for(register int i = 1; i <= n; i++) scanf("%s" , s) , insert(i);
get_fail();
scanf("%s" , s) , calc();
for(register int i = 1; i <= n; i++) printf("%d\n" , ans[map[i]]);
}
最新文章
- Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
- (转)对博士学位说永别 by 王珢
- Ubuntu上安装Robomongo及添加到启动器
- IOS第一天
- 微信商城系统与手机APP的优势对比!
- PHP模拟发送POST请求之一、HTTP协议头部解析
- 第二部分 Mongodb增删改查
- sql 通过游标 拆分xml结构
- php5.3之前版本升级至5.3以及更高版本后部分语法简单归纳
- 一些时间的概念与区分(UTC、GMT、LT、TAI等)
- Real Boxing 2
- Shell根据年月日创建文件夹
- LinkedIn第三方登录
- [Javascript] Call Stack
- RS-232通信原理
- ASPxGridview必须设置ShowVerticalScrollBar为true才能动态改变高度。。。
- php中表单提交复选框与下拉列表项
- 使用http -server 搭建本地简易文件服务器
- 第3章 Data语意学
- JavaScript栈和队列
热门文章
- odoo关于计算字段store=True时导致的安装/更新时间较长问题的解决方案
- 【重难点】函数式接口、函数式编程、匿名内部类、Lambda表达式、语法糖
- 模板层之标签 自定义过滤器及标签 模板的继承与导入 模型层之前期准备 ORM常用关键字
- 用python 协程 爬百度小说西游记
- Spring MVC的运行流程
- echarts图表配置
- antDesign 【NG-ZORRO、Angular】日期选择框时间段nz-range-picker设置默认选择日期及限制日期可选范围
- Random概述和基本使用-Random生成指定范围的随机数
- By not providing ";FindQt5.cmake"; in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by ";Qt5";, but CMake did not find one.
- 朋友圈那串神秘字符背后的开源项目「GitHub 热点速览」