hdu 2222 Keywords_ac自动机模板
题意:给你n个单词,再给你一串字符,求在字符中有多少个单词出现过
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10010
#define MAXLEN 1000010
struct node{
node *child[26];
node *fail;
int count;
void init(){
for(int i=0;i<26;i++)
child[i]=NULL;
count=0;
fail=NULL;
}
}*q[50*N];
node *root;
int head,tail;
void insert(char *str){//构建前缀树
node *now,*next;
int i=0,t;
now=root;
while(str[i]){
t=str[i]-'a';
if(now->child[t]!=NULL)
now=now->child[t];
else{
next=new node;
next->init();
now->child[t]=next;
now=next;
}
i++;
}
now->count++;
}
void build_fail(){
int i;
node *now,*p;
head=tail=0;
now=root;
q[tail++]=now;
while(head<tail){
now=q[head];
for(i=0;i<26;i++){
if(now->child[i]==NULL) continue;
if(now==root) now->child[i]->fail=root;
else{
p=now->fail;
while(p!=NULL){
if(p->child[i]!=NULL){
now->child[i]->fail=p->child[i];
break;
}
p=p->fail;
}
if(p==NULL) now->child[i]->fail=root;
}
q[tail++]=now->child[i];
}
head++;
}
}
int query(char *str){
node *now,*temp;
int i,t,cnt;
i=cnt=0;
now=root;
while(str[i]){
t=str[i]-'a';
while(now->child[t]==NULL&&now!=root) now=now->fail;
now=now->child[t];
if(now==NULL) now=root;
temp=now;
while(temp!=NULL&&temp->count!=-1){
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
i++;
}
return cnt;
}
int main(int argc, char** argv) {
int t,n;
char str[MAXLEN];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
root=new node;
root->init();
while(n--){
scanf("%s",str);
insert(str);
}
build_fail();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}
hdu 2222 Keywords_ac自动机模板的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- 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自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
- HDU 2222 ----AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
随机推荐
- java生成json字符串的方法
例1:将map对象添加一次元素(包括字符串对.数组),转换成json对象一次. 代码: package com.json; //这是使用org.json的程序: import java.util.Ha ...
- kibana 版本kibana-4.3.1 修改地图
进入到安装目录下的src/ui/public/vislib/visualizations/目录 1.编辑_map.js文件 1 2 //url: 'https://otile{s}-s.mqcdn.c ...
- 场景示例 Nginx 访问日志
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr [$ti ...
- bzoj1689 [Usaco2005 Open] Muddy roads 泥泞的路
Description Farmer John has a problem: the dirt road from his farm to town has suffered in the recen ...
- Machine Learning - Lecture 16
Reinforcement Learning (R.L.) ① MDPs (Markov Decision Processes) ② Value Functions ③ Value Iteration ...
- 所闻所获5:关于iOS的证书
去年做ondine时,被iOS的证书搞得很是头大,做完了之后感觉一片混乱,印象也不是很深.最近又发布了meditashayne,个人的第二个App,也就重温了一下证书的一些相关操作.这一次的理解比较深 ...
- Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间
1.问:知道程序执行时间我们能够做什么? 在<C++应用程序性能优化>一书中,假设大家读过相信大家一定对性能优化这一块很上心,文中总是对优化前后的时间对照很直观给我们一个感受. 那么我们怎 ...
- hdu1753()模拟大型实景数字相加
标题信息: 手动求大的实数在一起, pid=1753">http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /** *大实数相加 ...
- yum安裝的包如何保留到本地
一, 很多时候,我们一直用yum安装的软件,但是毫无疑问,很多人都会想yum安装的软件的包存放在哪里了呢? 这是因为yum默认并不保存你所安装的包,那么如何才能保留安装的软件包呢? 方法很简单:修改y ...
- Git使用过程
Git-------目前世界上最先进的分布式版本控制系统(没有之一) 什么是版本控制系统? 说简单点,就是一个文件,对其增加.删除.修改都可以被记录下来,不仅自己可以修改,其他人也可以进行修改 每次对 ...