HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222
KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法。主要由Trie和KMP的思想构成。
题意:输入N个模式串,再给出一个文本串,求文本串里出现的模式串数目。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 500001
/*
一个模式串的某个字符匹配失败的时候,就跳到它的失败指针上继续匹配,重复上述操作,直到这个字符匹配成功,所以失败指针一定满足一个性质,它指向的一定是某个串的前缀,并且这个前缀是当前结点所在前缀的后缀,而且一定是最长后缀。
*/
struct Ac_DFA
{
int next[N][]; //一开始这里用了char结果MLE
int val[N], size, root, fail[N]; int creat() { //构造新节点
for(int i = ; i < ; i++) {
next[size][i] = -;
}
val[size] = ;
return size++;
} void init() {
size = ;
root = creat();
} void insert(char s[]) { //插入模式串
int len = strlen(s);
int now = root;
for(int i = ; i < len; i++) {
int c = s[i] - 'a';
if(next[now][c] == -) {
next[now][c] = creat();
}
now = next[now][c];
}
val[now]++;
} void build() { //构造fail函数
queue<int> que;
while(!que.empty()) que.pop();
for(int i = ; i < ; i++) { //初始化
if(next[root][i] == -) { //如果没有边就补上去
next[root][i] = root;
} else {
fail[next[root][i]] = root; //有边的话第一个结点指向root
que.push(next[root][i]);
}
}
while(!que.empty()) {
int now = que.front(); que.pop();
for(int i = ; i < ; i++) {
if(next[now][i] == -) {
next[now][i] = next[fail[now]][i]; //如果没有边构造一条边出来
// 构造的边是fail指针指向的节点的出边
} else {
fail[next[now][i]] = next[fail[now]][i]; //有边fail就指向与目前的相同结点(以目前匹配的串的最长后缀为前缀)的一条边
que.push(next[now][i]);
}
}
}
} int query(char s[]) {
int len = strlen(s);
int ans = ;
int now = root;
for(int i = ; i < len; i++) {
now = next[now][s[i] - 'a'];
int tmp = now;
while(tmp != root) {
ans += val[tmp];
val[tmp] = ;
tmp = fail[tmp]; // KMP思想:当前匹配失败,沿着失配边走看有没有能够匹配的串
}
}
return ans;
}
}; char s[];
Ac_DFA ac; int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
ac.init();
for(int i = ; i < n; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
int ans = ac.query(s);
printf("%d\n", ans);
}
return ;
}
HDU 2222:Keywords Search(AC自动机模板)的更多相关文章
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- 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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
随机推荐
- jose4j / JWT Examples
jose4j / JWT Examples View History JSON Web Token (JWT) Code Examples Producing and consuming a sign ...
- RMAN-06059: expected archived log not found, loss of archived log compromises recoverability
归档日志被物理删除后执行rman操作报错: RMAN> backup database plus archivelog; Starting backup at -JUL- :: current ...
- 使用RMAN对控制文件进行restore
控制文件的默认备份格式是: c-IIIIIIIIII-YYYYMMDD-QQ 其中: c:表示控制文件 IIIIIIIIII:表示DBID YYYYMMDD:备份的时间戳 QQ:16进制的序列号,从0 ...
- sh.status()
mongos> sh.status({"verbose":1})sh.status({"verbose":1}) --- Sharding Status ...
- 怎么设置task的最大线程数
//-------------------------------------------------------------------------- // // Copyright (c) Mic ...
- JQuery Delay Hover效果
CSS代码 .tbui_aside_float_bar { position: fixed; left: 50%; bottom: 120px; margin-left: 608px; border- ...
- Swift实战-豆瓣电台(六)视图跳转,传参及回跳
youku观看地址:http://v.youku.com/v_show/id_XNzMxMzQ3MDcy.html 要点 在ChannelController里面声明一个代理 这个代理遵循我们自定义的 ...
- !!20160829——多次错误的T+0操作
- Python学习总结19:类(二)
参考:http://python.jobbole.com/82308/ 继承和__slots__属性 1. 继承 在Python中,同时支持单继承与多继承,一般语法如下: class SubCl ...
- Eclipse安装Ruby插件应该注意的几点
http://www.aptana.com/products/studio3/success_plugin.html Installing via Eclipse Please copy the fo ...