AC自动机(Keywords Search)
题目链接:https://cn.vjudge.net/contest/280743#problem/A
题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中是模式串的子串的有多少个?
具体思路:AC自动机模板题,先说一下各个数组的作用吧,ch数组是字典树中的数组,val也是字典树中的数组,储存的是从根节点到当前这个节点是不是有字符串。fail和last就是AC自动机中的数组了,fail数组的作用就是从根节点到当的节点形成的字符串的后缀是一个新的路径的前缀,能够在匹配失败的时候,通过指针的跳动来节省时间。last数组的作用就是在求和的时候方便记录,如果当前节点有字符串的话,直接通过指针的跳动能够避免当前节点没有字符串的情况。
AC代码:
#include<iostream>
#include<stack>
#include<stdio.h>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
# define ll long long
const int maxn = 2e5+;
const int maxm = 1e6+;
char str[maxm];
int tot,ch[maxn][],val[maxn];
int fail[maxn],last[maxn];
void add()
{
int p=;
int len=strlen(str);
for(int i=; i<len; i++)
{
int u=str[i]-'a';
if(!ch[p][u])
ch[p][u]=++tot;
p=ch[p][u];
}
val[p]++;
}
void getfail()
{
queue<int>q;
for(int i=; i<; i++)
{
if(ch[][i])
q.push(ch[][i]);
}
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=; i<; i++)
{
int u=ch[top][i];
if(u==)
continue;
q.push(u);
int v=fail[top];
while(v&&ch[v][i]==)
v=fail[v];
fail[u]=ch[v][i];
last[u]=val[fail[u]] ? fail[u] : last[fail[u]];
}
}
}
int cal(int t)
{
int ans=;
while(t)
{
ans+=val[t];
val[t]=;
t=last[t];
}
return ans;
}
int getans()
{
int ans=;
int len=strlen(str);
int p=;
for(int i=; i<len; i++)
{
int u=str[i]-'a';
while(p&&ch[p][u]==)
p=fail[p];
p=ch[p][u];
if(val[p])
ans+=cal(p);
else if(fail[p])
ans+=cal(fail[p]);
}
return ans;
}
void init()
{
for(int i=; i<tot; i++)
{
fail[i]=,last[i]=,val[i]=;
for(int j=; j<; j++)
{
ch[i][j]=;
}
}
tot=;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%s",str);
add();
}
getfail();
scanf("%s",str);
int ans=getans();
printf("%d\n",ans);
}
return ;
}
AC自动机(Keywords Search)的更多相关文章
- AC自动机---Keywords Search
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...
- AC日记——Keywords Search hdu 2222
2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu----(2222)Keywords Search(ac自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- underscore.js源码解析(一)
一直想针对一个框架的源码好好的学习一下编程思想和技巧,提高一下自己的水平,但是看过一些框架的源码,都感觉看的莫名其妙,看不太懂,最后找到这个underscore.js由于这个比较简短,一千多行,而且读 ...
- ffmpeg格式转换
遇到有些wav文件在ubuntu下无法打开的情况,可以使用ffmpeg进行格式转换即可 ffmpeg -i 0.wav test.wav
- 解决eclipse中mybatis的xml配置文件无代码提示问题
https://blog.csdn.net/IRainReally/article/details/81743506
- JetBrains系列WebStorm等中文输入法无法跟随光标的问题的解决办法
参考:https://blog.csdn.net/wang414300980/article/details/79537875 电脑配置: 解决这个问题的思路就是修改启动软件的JDK,有以下几个方法: ...
- Linux 文件系统介绍
目录 1.Linux 分区简介 2.文件的类型 3.文件的属性与权限 4.直达底部 一.Linux 分区简介 与 windows 通过 盘符管理各个分区不同,Linux把所有设备和文件都当作文件来管理 ...
- [转帖] K8S 常用命令
k8s常用命令 原贴地址 查看集群信息: [root@kubernetes-master pods]# kubectl cluster-info kubectl cluster-info展示结果 k ...
- Java时间的转换
String DATE_FORMAT = "yyyy-MM-dd"; LocalDate localDate = LocalDate.now(); long startTime = ...
- Java 8新特性之Date/Time(八恶人-4)
Mannix‘s Marauders -Chris Mannix 曼尼克斯掠夺者 曼尼克斯·克里斯 “I'm the new sheriff of Red Rock.” “我是红石镇的新任警长” ...
- 洛谷P3950 部落冲突(LCT)
洛谷题目传送门 最无脑LCT题解,Dalao们的各种算法都比这个好多啦... 唯一的好处就是只管码代码就好了 开战cut,停战link,询问findroot判连通性 太无脑,应该不用打注释了.常数大就 ...
- SpringBoot入坑指南之六:使用过滤器或拦截器
在Web应用中,常常存在拦截全部或部分请求进行统一处理的应用场景,如权限校验.参数校验.性能监控等. 在SpringMVC框架中,我们可以通过过滤器或拦截器实现相关功能,spring-boot-sta ...