题目链接

统计一段字符串中有多少个模板串在里面出现过

#include<bits/stdc++.h>
using namespace std; const int N=; struct Trie
{
int next[N][];
int fail[N];// fail[i]表示i结点//
int end[N]; // end[i]表示以i结点为结尾的模式串个数
int L,root;
int newnode()
{
for(int i=;i<;i++)
next[L][i]=-;
end[L++]=;
return L-;
}
void init()
{
L=;
root=newnode();
}
void insert(char buf[])
{
int now=root;
for(int i=;buf[i];i++)
{
int ch=buf[i]-'a';
if(next[now][ch]==-)
next[now][ch]=newnode();
now=next[now][ch];
}
end[now]++;
}
void build()
{
queue<int> Q;
fail[root]=root;
for(int i=;i<;i++)
{
if(next[root][i]==-)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];//
else
{
fail[next[now][i]]=next[fail[now]][i];//
Q.push(next[now][i]);
}
}
}
}
int query(char buf[])
{
int now=root;
int temp,ret=;
for(int i=;buf[i];i++)
{
int ch=buf[i]-'a';
now=next[now][ch];
temp=now;
while(temp!=root)
{
ret+=end[temp];
end[temp]=; //不作重复计数
temp=fail[temp];
}
}
return ret;
}
}ac; char buf[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char str[];
int n;
ac.init();
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",str),ac.insert(str);
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
}

hdu_2222: Keywords Search(AC自动机模板题)的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  2. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  4. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  6. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  7. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  8. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  9. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

随机推荐

  1. Java将头像图片保存到MySQL数据库

    在做头像上传的过程中通常是将图片保存到数据库中,这里简单介绍一中将图片保存到数据库的方法: jsp代码: <div> <input class="avatar-input& ...

  2. lucene全文搜索之一:lucene的主要功能和基本结构(基于lucene5.5.3)

    前言:lucene并不是像solr或elastic那样提供现成的.直接部署可用的系统,而是一套jar包,提供了一些常见语言分词.构建索引和创建搜索器等等功能的API,我们常用到的也就是分词器.索引目录 ...

  3. react-native学习(RN)--之Window环境下搭建环境配置

    react-native以后会更火的,自从2015年facebook开源了Android 一.安装java 二.安装Android Studio 三.安装react-native需要的Android ...

  4. Mybatis配置(一)

    1.导入Mybatis包 2.得到SqlSession来访问数据库 /** * 访问数据库 */public class DBAccess {          public SqlSession g ...

  5. redis bitcount variable-precision swar算法

    花了不到一周的时间看完了一本reids设计与实现的书,感觉整体的设计有些地方的确很巧妙,各个结构之间联系的非常紧密,但是很简单,逻辑性的没有太多,但是学到了一个bitcount计数1的方法比较巧妙,记 ...

  6. go 基础语法

    时间有限,简单记一些常用的,麻烦的不写了 定义变量:可以连续定义,也可以单个定义 var a int    int类型 var a="ds"   默认string类型 a:=&qu ...

  7. (cljs/run-at (->JSVM :browser) "语言基础")

    前言  两年多前知道cljs的存在时十分兴奋,但因为工作中根本用不上,国内也没有专门的职位于是搁置了对其的探索.而近一两年来又刮起了函数式编程的风潮,恰逢有幸主理新项目的前端架构,于是引入Ramda. ...

  8. oracle删除数据后表空间仍过大问题解决方法

    -----亲测有效------- --一.备份原始数据库库--1.备份空表--在plsql里面执行一下这句话 然后把结果集 再执行一把 再导数据select 'alter table '||table ...

  9. phpcmsV9手机站内容页有时内容不显示

    phpcmsV9手机站内容页有时内容不显示,修改的办法是: 在文件phpcms\modules\wap\index.php 中 屏蔽第119行,即如下内容 //$content = $contentp ...

  10. C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...