HDU 3065 AC自动机 裸题
中文题题意不再赘述
注意 失配数组 f 初始化一步到位
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; #define N 2000100
#define maxnode 50010
#define sigma_size 26
struct node{
char name[55];
int num;
}S[1101];
struct Trie{
int ch[maxnode][sigma_size];
int val[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));
val[0] = 0;
}
int idx(char c){
return c-'A';
} void Creat(char *s, int num){
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] = num ; //u若是单词结尾则为 +1
} int find(char *T){
int len = strlen(T);
int j = 0;
for(int i = 0; i < len; i++){
if(T[i]<'A' || T[i]>'Z'){ j = 0; continue;}
int c = idx(T[i]);
j = ch[j][c];
int temp = j;
while(temp && val[temp]){
S[ val[temp] ].num ++;
temp = f[ temp ];
}
} return 0;
} void getFail(){
queue<int> q;
//初始化队列
for(int c = 0; c < sigma_size; c++)
if(ch[0][c])q.push(ch[0][c]); while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma_size; c++){
int u = ch[r][c];
if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v]; //沿失配边追溯到可以匹配的(非原点)位置
f[u] = ch[v][c];
}
}
}
};
Trie ac;
char S1[N]; int main(){
int n;
while(~scanf("%d",&n)){
ac.init();
for(int i = 1; i <= n; i++){
scanf("%s",S[i].name);
S[i].num = 0;
ac.Creat(S[i].name, i);
}
ac.getFail(); scanf("%s",S1);
ac.find(S1); for(int i = 1; i <= n; i++)
if(S[i].num)
printf("%s: %d\n",S[i].name, S[i].num); }
return 0;
}
HDU 3065 AC自动机 裸题的更多相关文章
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- 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 ...
随机推荐
- Ubuntu系统、开发环境配置
在VMware10下安装成功了Ubuntu 13.10桌面版,刚安装完需要配置很多内容,下面为记录: 1. 更新源: 想了解更新地址的可以查看apt-get的源列表文件 $ sudo gedit /e ...
- Eyeshot Ultimate 学习笔记(1)
在Winform项目中用到3D技术,这是在做项目一段时间以来第一次,还是指定的3D控件Eyeshot Ultimate,这个控件名称用度娘搜索,竟然毫无结果,不知道是没有人用过还是觉得该控件过于简单, ...
- php中的NOTICE 的错误解决方法
PHP新手NOTICE错误,特此写给那些遇到和我一样错误的朋友. 刚学习PHP,不久 最近在整留言板,刚才遇到个问题. 页面中,好多类似 Notice: in D:\wamp\www\study\ ...
- C语言的编译过程和GCC编译参数
C语言的编译一般有三个步骤: 预编译: gcc -E -o a.e a.c 预编译a.c文件,生成的目标文件名为a.e 预编译就是将include包含的头文件内容替换到C文件中,同时删除代码中没用的注 ...
- [BZOJ 1221] [HNOI2001] 软件开发 【费用流 || 三分】
题目链接:BZOJ - 1221 题目分析 算法一:最小费用最大流 首先这是一道经典的网络流问题.每天建立两个节点,一个 i 表示使用毛巾,一个 i' 表示这天用过的毛巾. 然后 i 向 T 连 Ai ...
- 1、Window10 Electron 开发环境搭建
1:首先安装nodejs C:\Users\yan>node -v v4.5.0 2:安装Electron C:\Users\yan>npm install --g electron-pr ...
- 【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常
尼玛,这个问题干扰了我两个星期!! 关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发 在读<<iText in Acti ...
- 用POLL的方式,没有跑出结果来,立此存照
咦,这些内容,和我以前看内核时的东东,对应起来了.. SELECT,POLL,EPOLL,非阻塞,异步之类的... 但我没有调出来.回家有空了可以看看,不用再敲打代码啦... #!/usr/bin/e ...
- h.264并行解码算法2D-Wave实现(基于多核非共享内存系统)
在<Scalable Parallel Programming Applied to H.264/AVC Decoding>书中,作者基于双芯片18核的Cell BE系统实现了2D-Wav ...
- Android 中LocalBroadcastManager的使用方式
Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...