HDU 2896 AC自动机 裸题
中文题题意不再赘述
注意字符范围是可见字符,从32开始到95
char c - 32
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
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;} #define N 10000
#define maxnode 57000
#define sigma_size 95
int pre[3];
struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int f[maxnode];
int sz;
void init(){
sz=1;
memset(ch[0],0,sizeof(ch[0]));
val[0] = 0;
}
int idx(char c){
return c-32;
} 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]){ memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c] = sz++; }
u = ch[u][c];
}
val[u] = num ; //u若是单词结尾则为 +1
} int find(char *T, int num){
int len = strlen(T);
int j = 0;
int fir = 0;
f[0] = 0;
for(int i = 0; i < len; i++){
int c = idx(T[i]); j = ch[j][c];
if(!j) j = ch[0][c];
int temp = j;
while(temp && val[temp]){ if(!fir) printf("web %d:", num); pre[fir++] = val[j];
if(fir == 3)break; temp = f[temp];
}
} if(fir){
sort(pre, pre+fir);
for(int k = 0; k < fir; k++)printf(" %d",pre[k]);
printf("\n");
}
return fir>0;
} void getFail(){
queue<int> q;
f[0] = 0;
//初始化队列
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],S2[N]; void InputString(){
gets(S1); }
int main(){ int n; while(~scanf("%d",&n)){
ac.init();
getchar();
for(int i = 1; i <= n; i++){
InputString();
ac.Creat(S1, i);
}
ac.getFail(); int ANS = 0;
scanf("%d",&n);
getchar();
for(int i = 1; i <= n; i++){
InputString();
ANS += ac.find(S1, i);
}
printf("total: %d\n",ANS);
}
return 0;
}
/*
3
a aa
bbb
ccc
2
a aabbbccc
bbaacc ans:
web 1: 1 2 3
total: 1 */
HDU 2896 AC自动机 裸题的更多相关文章
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- HDU 3065 AC自动机 裸题
中文题题意不再赘述 注意 失配数组 f 初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- 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 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机裸题
HDU 2222 Keywords Search 模板题.对模式串建立AC自动机然后在trie树上找一遍目标串即可. # include <cstdio> # include <cs ...
随机推荐
- sql 学习笔记 p46
交换行 update tab1 set c1=c2,c2=c1 .说明sql是临时表的存储,在查询出来的结果为决定前,可以随意操纵临时表中的列 update tab set c1=c1+(selec ...
- (转)ThinkPHP自定义标签
第一:在当前应用下的Conf文件夹中config.php加两个配制项: 'TAGLIB_LOAD' => true,//加载标签库打开 'APP_ ...
- Android Studio使用教程图文详解
谷歌表示Android Studio 1.0 能让开发者“更快更有生产力”,并认为它可以代替 Eclipse,同时为Eclipse 用户提供迁移步骤.代码自动提示.运行响应速度.都比Eclipse来的 ...
- 重新开始学习javase_集合_List
一,List之ArrayList(转:http://blog.csdn.net/zheng0518/article/details/42198205) 1. ArrayList概述: ArrayLis ...
- uvalive 5721 Activation (概率dp+方程)
题目链接:http://vjudge.net/problem/viewProblem.action?id=24999 主要思想就是解方程的思想. 二维dp应该很容易想到,就是当前位置加队伍长度. dp ...
- Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- C++11中int,float,double与string的转化
在C++11中可以使用std::to_string()函数将数值转换为string格式,十分方便. 以下部分来选自cplusplus.com. std::to_string string to_str ...
- [FindBugs分析记录]Class defines clone() but doesn't implement Cloneable
官网解释: This class defines a clone() method but the class doesn't implement Cloneable. There are some ...
- set_time_limit() 控制页面运行时间
当你的页面有大量数据时,建议使用set_time_limit()来控制运行时间,默认是30s,所以需要你将执行时间加长点,如 set_time_limit(300) ,其中将秒数设为0 ,表示持续运 ...
- srpm包的编译方式
基本说明:后缀仅为rpm的包如xxxxx.rpm称作为二进制包 ------ 可以直接安装到架构匹配的系统上; 后缀为src.rpm的包如webkitgtk-2.4.7-1.fc21.src.rpm称 ...