HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法。
题意:求目标串中出现了几个模式串。
使用一个int型的end数组记录,查询一次。
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; const int maxw = 50 * 10000 + 10;
const int sigma_size = 26;
const int maxl = 1000000 + 10; struct Trie{
int next[maxw][sigma_size],fail[maxw],end[maxw];
int root,L;
int newnode(){
for(int i=0;i<sigma_size;i++)
next[L][i]=-1;
end[L++]=0;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void insert(const char *buf){
int now=root,len=strlen(buf);
for(int i=0;i<len;i++){
if(next[now][buf[i]-'a']==-1)
next[now][buf[i]-'a']=newnode();
now=next[now][buf[i]-'a'];
}
end[now]++;
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0;i<sigma_size;i++)
if(next[root][i]==-1)
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=0;i<sigma_size;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int query(const char *buf){
int now=root,len=strlen(buf);
int res=0;
for(int i=0;i<len;i++){
now=next[now][buf[i]-'a'];
int tmp=now;
while(tmp!=root){
res+=end[tmp];
end[tmp]=0;
tmp=fail[tmp];
}
}
return res;
}
}; Trie ac;
char buf[maxl]; int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ac.init();
for(int i=0;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
int ans=ac.query(buf);
printf("%d\n",ans);
}
return 0;
}
HDU 2222 AC自动机模版题的更多相关文章
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
随机推荐
- 51Nod1802 左偏树计数
题目大意 求$n$个点的无标号左偏树个数 既然你都点进来了,那么估计也是奔着题解来的.... 废话少说.... 首先,左偏树有这么一些性质 设最右链长度为$r[p]$ 1.左偏树的子树仍然是左偏树 2 ...
- [转]php-fpm - 启动参数及重要配置详解
约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...
- NOIP 解题有感
算法方面: 在搜索问题上,包括贪心等没有固定算法的题目,还有输出格式(包括输入格式)特别容易出错.这也是解题选手的弱点. 1.做搜索题把步骤先用文字写下来,再转换成代码,以避免敲代码时疏漏某个条件. ...
- 【8.30校内测试】【找规律模拟】【DP】【二分+贪心】
对于和规律或者数学有关的题真的束手无策啊QAQ 首先发现两个性质: 1.不管中间怎么碰撞,所有蚂蚁的相对位置不会改变,即后面的蚂蚁不会超过前面的蚂蚁或者落后更后面的蚂蚁. 2.因为所有蚂蚁速度一样,不 ...
- java中重载(overload)与重写(override)的区别
方法重载(overload): 方法重载就是在一个类中可以创建多个方法,它们具有相同的名字,但是具有不同的参数和不同的定义,调用方法时通过传递给它们的不同参数个数和参数类型来决定具体使用哪个方法,这就 ...
- VK Cup 2016 - Qualification Round 2 A. Home Numbers 水题
A. Home Numbers 题目连接: http://www.codeforces.com/contest/638/problem/A Description The main street of ...
- 2015 UESTC 搜索专题A题 王之迷宫 三维bfs
A - 王之迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
- 'NSUnknownKeyException', reason:....etValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
erminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MainTableViewControl ...
- HF Reader
- MYSQL GDB SHELL
http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201141165216974/