模板

\(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]]);
}

最新文章

  1. Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
  2. (转)对博士学位说永别 by 王珢
  3. Ubuntu上安装Robomongo及添加到启动器
  4. IOS第一天
  5. 微信商城系统与手机APP的优势对比!
  6. PHP模拟发送POST请求之一、HTTP协议头部解析
  7. 第二部分 Mongodb增删改查
  8. sql 通过游标 拆分xml结构
  9. php5.3之前版本升级至5.3以及更高版本后部分语法简单归纳
  10. 一些时间的概念与区分(UTC、GMT、LT、TAI等)
  11. Real Boxing 2
  12. Shell根据年月日创建文件夹
  13. LinkedIn第三方登录
  14. [Javascript] Call Stack
  15. RS-232通信原理
  16. ASPxGridview必须设置ShowVerticalScrollBar为true才能动态改变高度。。。
  17. php中表单提交复选框与下拉列表项
  18. 使用http -server 搭建本地简易文件服务器
  19. 第3章 Data语意学
  20. JavaScript栈和队列

热门文章

  1. odoo关于计算字段store=True时导致的安装/更新时间较长问题的解决方案
  2. 【重难点】函数式接口、函数式编程、匿名内部类、Lambda表达式、语法糖
  3. 模板层之标签 自定义过滤器及标签 模板的继承与导入 模型层之前期准备 ORM常用关键字
  4. 用python 协程 爬百度小说西游记
  5. Spring MVC的运行流程
  6. echarts图表配置
  7. antDesign 【NG-ZORRO、Angular】日期选择框时间段nz-range-picker设置默认选择日期及限制日期可选范围
  8. Random概述和基本使用-Random生成指定范围的随机数
  9. By not providing &quot;FindQt5.cmake&quot; in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by &quot;Qt5&quot;, but CMake did not find one.
  10. 朋友圈那串神秘字符背后的开源项目「GitHub 热点速览」