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 ...
随机推荐
- scrollview背景头部拉伸
a - (void)viewDidLoad { [super viewDidLoad]; self.tableView.contentInset = UIEdgeInsetsMake(kImageOr ...
- 笔记007:对象——RegExp正则表达式对象
1.RegExp 一个用于匹配的模式文本 用0个或多个修饰符描述的匹配模式细节 RegExp对象的创建形式 用内建构造器创建 var re = new RegExp("j.*t&quo ...
- OpenLDAP,一登录系统就修改密码
http://guodayong.blog.51cto.com/263451/d-2 郭大勇的博客 1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepat ...
- java file类的常用方法和属性
1 常用方法 a.createNewFile方法 public boolean createNewFile() throws IOException 该方法的作用是创建指定的文件.该方法只 ...
- VMware Workstation+Linux+Xshell+Xftp+MySQL+SQLyog 配置
这些天在搞这些个东西做项目,配置较繁,这里记下安装过程中的要点. 1.VMware Workstation 主要是 NAT 方式联网的问题,详述如下,来自网络. NAT 配置那里注意网关,虚拟机中网关 ...
- Java 枚举7种常见用法
(转)原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型--枚举.在 Java ...
- 使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空
使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空 添加 alter table table_name add field_name field_type; 添加,并设置默认值,及非 ...
- SQL三大范式三个例子搞定
第一范式(1NF) (必须有主键,列不可分) 数据库表中的任何字段都是单一属性的,不可再分 create table aa(id int,NameAge varchar(100)) insert aa ...
- sql脚本查询日期时间段日期
---列举指定时间月份DECLARE @date1 VARCHAR(10) , @date2 VARCHAR(10)SET @date1 = '2010-01-01'SET @date2 = '201 ...
- PHP获取页面执行时间的方法
一些循环代码,有时候要知道页面执行的时间,可以添加以下几行代码到页面头部和尾部: 头部: <?php $stime=microtime(true); 尾部: $etime=microtime(t ...