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 ...
随机推荐
- Android 巧妙实现图片和文字布局
之前写过一个博客是关于实现图片和文字左右或者上下布局的方法, 下面是博客的主要内容: 布局文件很简单,用来展示RadioButton的使用方法. 1 <?xml version="1. ...
- 05DotNet基本常用类库
1.String成员方法(常用) bool Contains(String str);判断字符串对象是否包含给定的字符串; bool StartsWith(String str);判断字符串对象是否以 ...
- 对C#泛型实例化对像--转
最近在编写一套开发框架结构主要应用.Net 3.5以上的框架开发与应用.在此框架中应用了较多的泛型.下面来讲讲对泛型的实例化,以代码为例,如: public class A { } public cl ...
- [Ext JS 4] 实战之Grid, Tree Gird 添加按钮列
引言 贴一个grid 的例子先: 有这样一个需求: 1. 给 Grid(or Tree Grid)添加一列, 这一列显示是Button. 点击之后可以对这一行进行一些操作 2. 这一列每一行对应的按钮 ...
- Python新手学习基础之数据结构-序列1
序列概念 序列,顾名思义就是有顺序的列,在Python里序列类型的数据结构包括字符串,列表和元组.既然都是序列类型,说明他们有很多共通点,他们的每一个元素都可以通过指定的偏移量方式(索引操作)来获得, ...
- C# ORM—Entity Framework 之Database first(数据库优先)&Model First(模型优先)(一)
一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...
- gets与scanf
gets与scanf 从功能上可以看出不同之处:1 终止条件不同: gets只有遇到\n时才会结束输入,而scanf遇到空格或制表符时,也会结束输入.比如输入"test string\n&q ...
- Scheme是什么、怎么自定义Scheme、JLRoutes的使用-备
转到移动端开发后居然现在才用到Scheme真是惭愧惭愧. URL Scheme是什么 相信大家都知道URL. http://www.apple.com就是一个URL. 而://之前的部分就称为Sche ...
- 转:搭建Hive的图形界面
原文来自于:http://blog.csdn.net/w13770269691/article/details/17353595 今天想使用一下Hive的图形化工具HWI,我的Hive是0.12.0版 ...
- poj 3020Antenna Placement
http://poj.org/problem?id=3020 #include<cstdio> #include<cstring> #include<algorithm& ...