AC自动机模板1
题目链接:https://www.luogu.org/problemnew/show/P3808
要注意的是一定要把len赋值strlen(s);不然超时超的自闭!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
const int N=1e6+;
void read(int &a)
{
a=;
int d=;
char ch;
while(ch=getchar(),ch>''||ch<'')
if(ch=='-')
d=-;
a=ch-'';
while(ch=getchar(),ch>=''&&ch<='')
a=a*+ch-'';
a*=d;
}
void write(int x)
{
if(x<)
putchar(),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
struct note
{
int to[],bz,nex;
}trie[N];
int tot;
char s[N];
void ins(char *s)
{
int t=;
int len=strlen(s);
for(re int i=;i<len;i++)
{
int x=s[i]-;
if(!trie[t].to[x])
trie[t].to[x]=++tot;
t=trie[t].to[x];
}
trie[t].bz++;
}
void built()
{
queue <int> q;
for(re int i=;i<=;i++)
if(trie[].to[i])
trie[trie[].to[i]].nex=,q.push(trie[].to[i]);
while(!q.empty())
{
int p=q.front();
q.pop();
for(re int i=;i<=;i++)
if(trie[p].to[i])
trie[trie[p].to[i]].nex=trie[trie[p].nex].to[i],q.push(trie[p].to[i]);
else
trie[p].to[i]=trie[trie[p].nex].to[i];
}
}
int solve(char *s)
{
int ans=,now=;
int len=strlen(s);
for(re int i=;i<len;i++)
{
now=trie[now].to[s[i]-'a'+];
for(re int j=now;j&&trie[j].bz!=-;j=trie[j].nex)
ans+=trie[j].bz,trie[j].bz=-;
}
return ans;
}
int main()
{
int n;
read(n);
while(n--)
{
scanf("%s",s);
ins(s);
}
trie[].nex=;
built();
scanf("%s",s);
cout<<solve(s)<<endl;
return ;
}
AC自动机模板1的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- react-native 搭建环境
1.安装 nodejs 配置环境变量 node -v npm -v 2.安装 javaSE 1.8以上 http://www.oracle.com/technetwork/java/javase/ar ...
- Ubuntu将网卡名称eno160改为eth0并且设置静态IP
修改配置文件/etc/default/grub GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" 设置生效 update-grub ...
- eclipse搭建j2ee
Tomcat环境变量设置,分别添加3个系统变量 CATALINA_BASE E:/tomcat7 CATALINA_HOME E:/tomcat7 CATALINA_TMPDIR Etomcat7/t ...
- 初探Spring Cloud Config
Spring Cloud Config提供了分布式系统中配置功能的服务端与客户端支持.对于不同环境的应用程序它的服务端提供了一种中心化的配置管理方式.并且其不仅适用于Spring的应用程序,其它语言开 ...
- 被Entity Framework Core的细节改进震撼了一下
今天用 SQL Server Profiler 查看 Entity Framework Core 生成的 SQL 语句时,突然发现一个细节改进,并且被它震撼了一下: exec sp_executesq ...
- redis集群,主从,持久化
1,单机版 先安装gcc yum install gcc-c++ 然后解压源码包,执行编译命令make(C语言写的,需要gcc环境),最后安装Redis,需要通过PREFIX指定安装路径make ...
- MAC apache服务器搭建
一.启动原本服务器 首先打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密).如下显示Apache的版本: 可以输入启动命令进行启动: sudo apa ...
- 查询大数据表的效率对比:Linq to SQL、Entity Framework、企业库存储过程、ADO.Net
最近因为要开发大数据量网站,特作比较. Linq to SQL 查询 记录数:399997Linq to SQL 查询 Milliseconds:1910视图查询 记录数:399997视图查询 Mil ...
- [development][tcp/ip][ids] 一个简单有参考价值的库 libnids
libhtp 中的例子, 可以通过libnids快速使用. 或者可以快速的写个sniffer. 支持三个功能 ip分片重组, tcp乱序重排, 端口扫描发现. 工程: https://github.c ...
- NumPy 广播机制(Broadcasting)
一.何为广播机制 a.广播机制是Numpy(开源数值计算工具,用于处理大型矩阵)里一种向量化数组操作方法. b.Numpy的通用函数(Universal functions) 中要求输入的两个数组sh ...