关键词匹配(Ac自动机模板题)
2772: 关键词匹配
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 10 Solved: 4
[Submit][Status][Web Board]
Description
Input
接下来N行,每行输入一个长度不超过50且全由小写字母组成的单词。
最后一行输入一个长度不超过1000000的字符串。
N≤10000
Output
Sample Input
5
she
he
say
shr
her
yasherhs
Sample Output
3
HINT
Source
题解:
裸Ac自动机,不会的可以去看看这个博客:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html;
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 1000005
#define maxm 1000005
using namespace std;
int n,tot,head,tail,son[maxm][],fai[maxm],sum[maxm],list[maxm];
char s[maxn];
void clear(){
tot=;
memset(son,,sizeof(son));
memset(sum,,sizeof(sum));
}
void insert(char *s){
int p=;
for (int i=;s[i];p=son[p][s[i]-'a'],i++) if (!son[p][s[i]-'a']) son[p][s[i]-'a']=++tot;
sum[p]++;
}
void failed(){
head=,tail=,list[]=,fai[]=-;
while (head!=tail){
int x=list[++head];
for (int ch=;ch<;ch++)
if (son[x][ch]){
list[++tail]=son[x][ch];
int p=fai[x];
while (p!=-&&!son[p][ch]) p=fai[p];
if (p==-) fai[son[x][ch]]=;
else fai[son[x][ch]]=son[p][ch];
}
}
}
void work(char *s){
int ans=;
for (int i=,p=;s[i];i++){
while (p&&!son[p][s[i]-'a']) p=fai[p];
p=son[p][s[i]-'a'];
for (int t=p;t;t=fai[t]) ans+=sum[t],sum[t]=;
}
printf("%d\n",ans);
}
int main()
{
cin>>n;//,clear();
for (int i=;i<=n;i++) scanf("%s",s+),insert(s);
failed();
scanf("%s",s+),work(s);
return ;
}
关键词匹配(Ac自动机模板题)的更多相关文章
- 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-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- AC自动机模板题
AC自动机学习博客 AC自动机理解要点: 1)fail指针指向的是每个节点,在字典树上和这个节点后缀相同的最长单词,每次都这样匹配,必定不会漏过答案. 2)字典树建立后,会在bfs求fail阶段把字典 ...
随机推荐
- MySQL 建表
SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for ` ...
- js实现类似于add(1)(2)(3)调用方式的方法
群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...
- Android面试经验1
1,java基本数据类型. Byte.short.int.long.float.double.char.boolean. 1 2 2 2 4 ...
- JavaScript 面向对象(一)
参考:http://www.iteye.com/topic/1123555
- UIScrollView代理方法
手拖拽后会调用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView [scrollView setContentOffset ...
- 【转】CentOS安装PF_RING(虚拟机)
1. 概述 PF_RING是Luca Deri发明的提高内核处理数据包效率,并兼顾应用程序的补丁,如Libpcap和TCPDUMP等,以及一些辅助性程序(如ntop查看并分析网络流量等). ...
- ural1521 War Games 2
War Games 2 Time limit: 1.0 secondMemory limit: 64 MB Background During the latest war games (this s ...
- ural1671 Anansi's Cobweb
Anansi's Cobweb Time limit: 1.0 secondMemory limit: 64 MB Usatiy-Polosatiy XIII decided to destroy A ...
- 伸展二叉树树(C#)
参考过好几篇关于将伸展树的代码,发现看不懂.看图能看懂原理.就尝试自己实现了下. 自顶向上的算法. using System; using System.Collections.Generic; us ...
- Quick Cocos2dx 场景转换问题
项目结构是这样子的: 主场景代码是这样子的: local MainScene = class("MainScene", function() return display.newS ...