hdu_2222: Keywords Search(AC自动机模板题)
统计一段字符串中有多少个模板串在里面出现过
#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自动机模板题)的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...
随机推荐
- cpp(第十三章)
1.动态(晚期)联编需要显示定义复制构造函数,赋值运算符,虚构函数. 2.纯虚类不能声明对象. 3.赋值运算符的特征标随类而异. 4.返回类型协变,重新定义继承的方法,应确保与原来的原型完全相同,但如 ...
- 用Backtrack进行渗透测试评估
Web应用程序的分析在渗透测试和漏洞评估中发挥了重要的作用.确定Web应用程序的正确信息(例如使用的插件,CMS类型等)都可以帮助测试者使用准确的漏洞来测试,能够降低整个渗透测试漏洞评估所花费的时间. ...
- 走进BFC
在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位: Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由 ...
- angular.js添加自定义服务依赖项方法
<!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...
- ADO.NET中的DataSet和DataReader
ADO.NET提供两个对象用于检索关系型数据并把它存储在内存中,分别是DataSet和DataReader.DataSet提供内存中关系数据的表现--包括表和次序.约束等表间的关系的完整数据集合.Da ...
- python 自定义回调函数
回调函数用起来比较爽.特别是在js中,满世界全是回调,那么在python中,怎么来优雅地实现自己的回调函数呢 下面贴一个我写的例子 class BaseHandler(object): def cra ...
- python 标准库 -- shutil
shutil shutil.move(src,dst) shutil.move('/tmp/20170223/new','/tmp/20170223/test') # 移动文件, 重命名等 shuti ...
- SICP-1.6-高阶函数
高阶函数 将函数作为参数 例如 def sum_naturals(n): total, k = 0, 1 while k <= n: total, k = total + k, k + 1 re ...
- maven 不同环境加载不同的properties 文件
http://haohaoxuexi.iteye.com/blog/1900568 //参考文章 实际项目中pom配置如下 <profiles> <profile> <i ...
- javascript 闭包 转载
http://www.jb51.net/article/24101.htm var name = "the window"; var object = { name:"m ...