/**
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L(L <= 106)目标串,求目标串出现了多少个模式串。
思路:ac自动机入门题。。直接插入查询。
唯一需要特殊考虑的是存在多个相同的字符串;相同的字符串会在字典书上覆盖原先的。
解决方法1:用map<string,int>标记同一种字符串。之后利用标记来统计。
解决方法2:用num[i]标记字典树上某个节点为结尾的字符串出现次数。之后统计的时候,如果是第一次统计它,那么加上它,然后置为-1表示
下次不需要再统计它了。 AC自动机好文章:http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
*/ ///解法1:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
cnt[val[j]] = ;
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
char t[][];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",t[i]);
ac.insert(t[i],i);
mp[string(t[i])] = i;///因为两个完全相同的字符串会覆盖原先的,所以用map标记属于同一个。这样可以都加到。
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[mp[string(t[i])]];
printf("%d\n",ans);
}
return ;
} /*
1
5
she
he
say
shr
her
yasherhs
*/ ///解法2:
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
int cnt[];
map<string,int> mp;
int num[maxnode];///统计在自动机上到达i节点的这个字符串的相同字符串的个数。
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int x)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
num[sz] = ;
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = x;
num[u]++;
} void find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
if(num[j]!=-){
cnt[val[j]] = num[j];
num[j] = -;
}
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[u] = ;}
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
ac.clear();
mp.clear();
for(int i = ; i <= n; i++){
scanf("%s",s);
ac.insert(s,i);
}
scanf("%s",s);
ac.getFail();
ms(cnt,);
ac.find(s);
int ans = ;
for(int i = ; i <= n; i++) ans += cnt[i];
printf("%d\n",ans);
}
return ;
} /*
2
5
she
he
say
shr
her
yasherhs
2
ab
ab
aba
*/

hdu2222 KeyWords Search AC自动机入门题的更多相关文章

  1. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  2. HD2222 Keywords Search(AC自动机入门题)

    然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...

  3. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  4. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  5. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  6. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  8. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  9. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

随机推荐

  1. 使用Promise

    Promise所要解决的问题:回调地狱 asyncTask1(data, function (data1){ asyncTask2(data1, function (data2){ asyncTask ...

  2. python学习笔记——正则表达式regex

    1 概述 1.1 定义 本质是由一系列字符和特殊符号组成的字串,用来表示一定规则的某一类字符串. 1.2 特点 正则表达式是一个独立的技术,其在多种编程语言中使用. 在python语言中的正则表达式模 ...

  3. oc Delegate

    把内部的状态通知给外界,我们可以制定一个变量,然后这个变量从外界来指定,之后我们可以通过变量去通知给外界有什么发生了. 按照上文讲的到新建一个protocol,名字为IPeople #import & ...

  4. Linux内核(2) - 分析内核源码如何入手(上)

    透过现象看本质,兽兽们无非就是一些人体艺术展示.同样往本质里看过去,学习内核,就是学习内核的源代码,任何内核有关的书籍都是基于内核,而又不高于内核的. 既然要学习内核源码,就要经常对内核代码进行分析, ...

  5. 【驱动笔记10】再谈IRP

    文章作者:grayfox作者主页:http://nokyo.blogbus.com原始出处:http://www.blogbus.com/nokyo-logs/34010655.html 这一节会对I ...

  6. System Generator简介

    前言 System generator 安装之后会在Simulin模块库中添加一些Xilinx FPGA专用的模块库,包括Basic Element,Communication,Control Log ...

  7. unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销

    unity3d 通过添加rigidBody来指明物体是动态的,以避免cache开销. 如果不添加rigidBody,则unity会认为它是静态的,会对物理计算进行cache,但如果此物体实际上tran ...

  8. CentOS安装Webmin

    解析:Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前 Webmin支持绝大多数的Unix系统,这些系统除了各种 ...

  9. VC++学习之多线程(2)

    创建一个线程,自然有一个对应的系统API来完毕.CreateThread这个函数就用来创建线程的. 各种參数的用途我就不多说了,这里直接贴一个我自己练习的样例 1.以下是一个创建一个线程的样例,当然, ...

  10. REST技术第三步 @BeanParam的使用

    我简介下rest中@BeanParam的使用 我们来做一个计算加法的服务. 提供两个參数a和b,计算出a+b. 參数都在URL里 就是类似这样的效果. 这次用上@BeanParam 首先写个POJO来 ...