hdu2222--Keywords Search+AC自己主动机模板
pid=2222">点击进入
KMP对模式串进行处理。然后就能够方便的推断模式串是否在目标串中出现了;这显示适合一个模式串多个目标串的情况。可是假设模式串有多个,这时假设还用KMP则须要对每一个串都进行一次处理,显然不是非常合适。事实上这时候能够将全部模式串建成一棵trie树。然后採用相似于kmp的方法计算出failed指针,也就能够方便的进行匹配了。事实上这就是ac自己主动机的思想。
代码例如以下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=500000+100;
struct Trie
{
///next数组表示的就是trie树
///fail数组记录了每一个节点的失败指针
///end数组记录了以当前节点作为结尾的字符串的个数
int next[maxn][26],fail[maxn],end[maxn];
///root表示trie的根节点。L表示当前trie的节点总数
int root,L;
int newnode()
{
for(int i=0;i<26;i++)
next[L][i]=-1;
end[L++]=0;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
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]++;
}
///採用bfs形式构造fail指针
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0;i<26;i++)
{
///假设当前节点的第i个儿子不存在
///则将其补上,为当前节点失败节点的第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<26;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(char buf[])
{
int len=strlen(buf);
int res=0;
int now=root;
for(int i=0;i<len;i++)
{
now=next[now][buf[i]-'a'];
int temp=now;
while(temp != root)
{
res+=end[temp];
end[temp]=0;
temp=fail[temp];
}
}
return res;
}
};
char str[maxn*2];
Trie ac;
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ac.init();
for(int i=0;i<n;i++)
{
scanf("%s",str);
ac.insert(str);
}
ac.build();
scanf("%s",str);
printf("%d\n",ac.query(str));
}
return 0;
}
hdu2222--Keywords Search+AC自己主动机模板的更多相关文章
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- AC自己主动机模板
AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- HDU 5384 Danganronpa (AC自己主动机模板题)
题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...
- 【HDU】病毒侵袭(AC自己主动机模板题)
AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- 洛谷 P4073 [WC2013]平面图
#include<bits/stdc++.h> using namespace std; ; typedef long double LD; ; ); int dcmp(LD x){ret ...
- 第五讲:Fast RTL-level verification
1.good code styles 2.+rad compile time switch for compile 1.了解VCS 的架构 <===这方便了解不多 parser / even ...
- ()-servlet.xml中剥离出的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)
1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...
- MindManager 设置默认Note字体大小
工具栏 Design > Notes Theme > Default Font
- SpringCloud源码地址
SpringCloud实战源代码 https://github.com/springcloud/spring-cloud-code.git
- xtu字符串 C. Marlon's String
C. Marlon's String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...
- NYOJ-673悟空的难题~~水题~~
悟空的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 自从悟空当上了齐天大圣,花果山上的猴子猴孙们便也可以尝到天上的各种仙果神酒,所以猴子猴孙们的体质也得到了很好的 ...
- [Istio]Kubernetes集群部署Istio 1.0
大部分内容都是可以根据https://istio.io/docs/setup/kubernetes/quick-start/来处理的,这里主要谈部署时一些细节的问题 首先,当我们按照 istio 官方 ...
- GO 语言周报【七月第 1 期】
TIOBE 七月排名 Go 进入前十 TIOBE 七月头条:Go 语言达到历史最高并进入前十.对于 Go 语言来说,这是一个里程碑时刻,我们可以更大胆地想象,它下一步的发展会达到怎样的高度.Go 是否 ...