hdu 2896 AC自动机
// hdu 2896 AC自动机
//
// 题目大意:
//
// 给你n个短串,然后给你q串长字符串,要求每个长字符串中
// 是否出现短串,出现的短串各是什么
//
// 解题思路:
//
// AC自动机,插入单词,构建自动机,然后查询就可以了。
//
// 感悟:
//
// 哎,自己AC自动机果然刚学,还只会个模板,自动机构没构建
// 都不知道。。。继续加油吧~~~FIGHTING! #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAX_NODE = 200000; const int SIGMA_SIZE = 131; struct Aho_Corasick{ int ch[MAX_NODE][SIGMA_SIZE];
int val[MAX_NODE];
int cnt[508];
int sz;
int last[MAX_NODE];
int f[MAX_NODE]; void clear(){
memset(cnt,0,sizeof(cnt));
} void init(){
sz = 1;
memset(ch[0],0,sizeof(ch[0]));
val[0] = 0;
} int idx(char c){
return c - 'a';
} void insert(char *s,int v){
int u = 0;
int n = strlen(s);
for (int i=0;i<n;i++){
//int c = idx(s[i]);
int c = 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] = v;
} void getfail(){
queue<int> que;
for (int c = 0; c < SIGMA_SIZE ; c++){
int u = ch[0][c];
if (u){
que.push(u);
last[u] = 0;
f[u] = 0;
}
}
while(!que.empty()){
int r = que.front();
que.pop();
for (int c = 0; c < SIGMA_SIZE ; c++){
int u = ch[r][c];
if (!u)
continue;
que.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]]; }
}
} void get_cnt(int u){
if (u){
cnt[val[u]]++;
get_cnt(last[u]);
}
} void query(char *s){
int u = 0;
int n = strlen(s);
for (int i=0;i<n;i++){
//char c = idx(s[i]);
char c = s[i];
while( u && !ch[u][c])
u = f[u]; u = ch[u][c]; if (val[u]){
get_cnt(u);
}
else if (last[u]){
get_cnt(last[u]);
}
}
} }ac; char str[10009]; int n; void print(int x,int &num){
int flag = 0;
for (int i=1;i<=n;i++){
if (ac.cnt[i]){
flag = 1;
break;
}
}
if (!flag)
return; printf("web %d:",x); for (int i=1;i<=n;i++){
if (ac.cnt[i]){
printf(" %d",i);
}
}
puts("");
num++; } void input(){
ac.init();
for (int i=1;i<=n;i++){
scanf("%s",str);
ac.insert(str,i);
}
int q;
int num = 0;
ac.getfail();
scanf("%d",&q);
int x = 1;
for (int i=1;i<=q;i++){
scanf("%s",str);
ac.clear();
ac.query(str);
print(i,num);
}
printf("total: %d\n",num);
} int main(){
//freopen("1.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
input();
}
}
hdu 2896 AC自动机的更多相关文章
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- hdu 5880 AC自动机
Family View Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5384 AC自动机
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...
随机推荐
- NSURLCache详解和使用
使用缓存的目的是为了使应用程序能更快速的响应用户输入,是程序高效的运行.有时候我们需要将远程web服务器获取的数据缓存起来,以空间换取时间,减少对同一个url多次请求,减轻服务器的压力,优化客户端网络 ...
- C. Dima and Salad 背包好题
http://codeforces.com/contest/366/problem/C 在n个物品中选出若干个,使得sum(a[i]) = k * sum(b[i]) 把问题转化一下就是,求sum(a ...
- SQL SERVER 分页查询
Sqlserver数据库分页查询一直是Sqlserver的短板. 但现在不是了. 自从有了它. 一口气上十楼. 官方语法说明示例: https://technet.microsoft.com/zh-c ...
- SQL中CONVERT日期不同格式的转换用法
SQL中CONVERT日期不同格式的转换用法 格式: CONVERT(data_type,expression[,style]) 说明:此样式一般在时间类型(datetime,smalldatetim ...
- 字节b换算kb/mb/gb/tb/pb
public static string HumanReadableFilesize(double size) { string[] units = new string[] { "B&qu ...
- 在css嵌套中的html的table里的字左右不对齐
[现象]AAAA与天数的数字左右不居中 <table border=1 align="center"> <tr> <td width="20 ...
- MSSQLSERVER服务无法启动的解决方案
MSSQLSERVER服务无法启动的解决方案 有时候sqlserver无法启动了,原因是mssqlserver服务没有启动,当你手动启动时,又出现服务无法响应的可恶错误提示... 笔者“有幸”遇到了, ...
- GOOGLE地图坐标拾取方法、GOOGLE地图获取坐标方法
方法一: 打开google地图-->查找目的地-->右键:此位置居中--> 打开IE浏览器(百度浏览器.GOOGLE浏览器.360浏览器均不行,只能在IE中获取),在IE浏览器的地址 ...
- easy ui 框架
Easy UI 准备工作(搭建) 1.在WebRoot 的目录下创建js 文件夹,在文件夹中倒入一下两个包 Jquery.easyui.min.js jquery.min.js 2.在WebRoot ...
- 网络-->监控-->交换机端口流量监控
一.取交换机端口流量OID 针对交换机接口速率在100M及以下: in方向:1.3.6.1.2.1.2.2.1.10 out方向:1.3.6.1.2.1.2.2.1.16 针对交换机端口速率在百兆以上 ...