HDU 2222 (AC自动机模板题)
题意:
给一个文本串和多个模式串,求文本串中一共出现多少次模式串
分析:
ac自动机模板,关键是失配函数
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 250010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct Trie{
int ch[N][],val[N],f[N],last[N],num;
void init(){
num=;
memset(ch,,sizeof(ch));
memset(val,,sizeof(val));
memset(f,,sizeof(f));
memset(last,,sizeof(last));
}
void print(int j){
if(j){
printf("%d: %d\n", j, val[j]);
print(last[j]);
}
}
void build(char *s){
int u=,len=strlen(s);
for(int i=;i<len;++i)
{
int v=s[i]-'a';
if(!ch[u][v]){
memset(ch[num],,sizeof(ch[num]));
ch[u][v]=num++;
}
u=ch[u][v];
}
val[u]++;
}
//失配函数
void getfail(){
queue<int>q;
for(int i=;i<;++i)
if(ch[][i])
q.push(ch[][i]);
while(!q.empty()){
int r=q.front();
q.pop();
for(int i=;i<;++i)
{
int u=ch[r][i];
if(!u)continue;
q.push(u);
int v=f[r];
while(v&&!ch[v][i])v=f[v];
f[u]=ch[v][i];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
int find(char *T){
int u=,len=strlen(T);
int total=;
for(int i=;i<len;++i){
int v=T[i]-'a';
while(u&&ch[u][v]==)
u=f[u];
u=ch[u][v];
int tmp=u;
while(tmp&&val[tmp]){
total+=val[tmp];
val[tmp]=;
tmp=f[tmp];
}
}
return total;
}
}ac;
int main()
{
char s[],T[];
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ac.init();
while(n--){
scanf("%s",s);
ac.build(s);
}
ac.getfail();
scanf("%s",T);
printf("%d\n",ac.find(T));
}
return ;
}
HDU 2222 (AC自动机模板题)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 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(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- 能量项链//区间DP
P1056 能量项链 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2006 提高组 第一道 描述 在Mars星球上,每个Mars人都随身佩 ...
- linux内核--进程与线程
http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...
- Unigui有用的网址
http://www.cnblogs.com/ChinaEHR/tag/Delphi/
- jQuery编程基础精华02(属性、表单过滤器,元素的each,表单选择器,子元素过滤器(*),追加方法,节点,样式操作)
属性.表单过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $("div[title=test]")选取title属性为 ...
- apache缓存
http://www.t086.com/article/4256 http://www.360doc.com/content/09/0928/13/41237_6551659.shtml
- BZOJ 2337 XOR和路径(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2337 题意:给定一个带权无向图.从1号点走到n号点.每次从当前点随机(等概率)选择一条相 ...
- C++ 中字符串标准输入的学习及实验
声明:下面实验中[]里面表示要输入里面的符号,[]符号本身并未输入 1.cin>> cin使用空白(空格.制表符.回车)来确定字符串的结束位置. cin会将换行符留在输入输出队列中. #i ...
- Linux查看所有用户用什么命令
用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令就能 ...
- HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- Self-Paced Training (3) - Docker Operations
AgendaTroubleshooting ContainersOverview of Security PracticesPrivate RegistryIntro to Docker Machin ...