题意:

问母串中出现多少个模式串

注意ac自动机的节点总数

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
inline int Max(int a,int b){return a>b?a:b;}
inline int Min(int a,int b){return a>b?b:a;}
int ANS; #define N 1000010
#define maxnode 250001
#define sigma_size 26 struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int last[maxnode];
int f[maxnode];
int sz;
void init(){
sz=1;
memset(ch,0,sizeof(ch));
memset(val, 0, sizeof(val));
memset(f,0,sizeof(f));
memset(last,0,sizeof(last));
}
int idx(char c){
return c-'a';
}
void print(int j){
if(j){
printf("%d: %d\n", j, val[j]);
print(last[j]);
}
}
void Creat(char *s){
int u = 0, len = strlen(s);
for(int i = 0;i < len;i++){
int c = idx(s[i]);
if(!ch[u][c])
ch[u][c] = sz++; u = ch[u][c];
}
val[u] ++;
}
void getFail(){
queue<int> q;
for(int i = 0; i<sigma_size; i++)
if(ch[0][i]) q.push(ch[0][i]); while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c<sigma_size; c++){
int u = ch[r][c];
if(!u)continue;
q.push(u);
int v = f[r];
while(v && ch[v][c] == 0) v = f[v]; //沿失配边走上去 如果失配后有节点 且 其子节点c存在则结束循环
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
void find(char *T){
int len = strlen(T), j = 0;
for(int i = 0; i < len; i++){
int c = idx(T[i]);
while(j && ch[j][c]==0) j = f[j];
j = ch[j][c]; int temp = j;
while(temp && val[temp]){
ANS += val[temp];
val[temp] = 0;
temp = f[temp];
}
}
}
};
Trie ac;
char S1[N];
int Slen; void InputString(){
scanf("%s",S1);
Slen = strlen(S1);
S1[Slen]='\0';
}
int main(){ int T,i,j,n;scanf("%d",&T); while(T--){
ac.init();
scanf("%d",&n);
while(n--){
scanf("%s",S1);
ac.Creat(S1);
}
ac.getFail();
InputString();
ANS = 0;
ac.find(S1); printf("%d\n",ANS); }
return 0;
}
/*
1
5
she
he
say
shr
her
yasherhs ans:
3 */

HDU 2222 AC自动机 裸题的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. hdu 2222(AC自动机模版题)

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

  3. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  4. HDU 3065 AC自动机 裸题

    中文题题意不再赘述 注意 失配数组 f  初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...

  5. HDU 2896 AC自动机 裸题

    中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...

  6. HDU 2222 AC自动机(模版题)

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

  7. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  8. AC自动机裸题

    HDU 2222 Keywords Search 模板题.对模式串建立AC自动机然后在trie树上找一遍目标串即可. # include <cstdio> # include <cs ...

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. SQLSERVER 触发器 将一个服务器上的数据库中数据插入到另一个服务器上的数据库中怎么做

    首先要执行 sp_addlinkedserver '服务器ip' 然后开始写语句 insert into ip.库名字.dbo.table select * from iserted

  2. SGU 224.Little Queens

    时间限制:0.75s 空间限制:6M 题意 n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数. Solution: 状态压缩+位运算  搜索. 首先我们从上往下逐行放置 ...

  3. 关于ASP.NET Session State Server

    最近公司开发的一个网站因为访问量增大,需要添加多台Web Server来进行负载均衡. 但是在做负载均衡前需要将一些原来固定存储在单台机器上的东西分离出来,使之能单独存在在一个独立的机器上,其中就有S ...

  4. js字符串倒序

    有的时候我们需要把字符串倒序. 比如“范坚强”的倒序就是“强坚范”. 如何对字符串进行倒序呢?你首先想到的方法就是生成一个栈,从尾到头依次取出字符串中的字符压入栈中,然后把栈连接成字符串. var r ...

  5. angular 实现总价满100折扣

    <div ng-controller="CartController"> <div ng-repeat="item in items"> ...

  6. [前端笔记]第三篇:JavaScript

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.代码存放位置 J ...

  7. linux配置记录

    今天想把开发环境都配置到linux环境来,所以用wubi挂载了个ubuntu系统(64位),因为不常使用linux所以把今天学到的一些 东西记下来以做备查. #1.   java环境配置 到oracl ...

  8. centos 下vmware 下添加硬盘到root

    ### #vmware 里找到硬盘拖大点...,如果不想从启动么,添加个新 #的也行.不过那个是另外的方法了 ###   #### ##找下硬盘添加在哪里 #### fdisk -l    //创建分 ...

  9. 关于Verilog 中的for语句的探讨

    在C语言中,经常用到for循环语句,但在硬件描述语言中for语句的使用较C语言等软件描述语言有较大的区别. 在Verilog中除了在Testbench(仿真测试激励)中使用for循环语句外,在Test ...

  10. 1. android

    http://blog.csdn.net/mirkerson/article/details/7238815