Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 318    Accepted Submission(s): 176

Problem Description

给你n个A串,m个B串,对每个A串,询问,这些B串们在该A串中一共出现过多少次

Input

样例个数

n m

接下来n个A串

接下来m个B串

Output

如问题描述,对每个A输出...

Sample Input
1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
 
Sample Output
1
1
0
3
7
 



Source
 
解题:AC自动机自动AC
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct trie {
int word[],fail,cnt;
void init() {
fail = cnt = ;
memset(word,-,sizeof word);
}
} dic[maxn];
int tot;
char str[];
void insertWord(int root,char *s) {
for(int i = ; s[i]; i++) {
int k = s[i]-'a';
if(dic[root].word[k] == -) {
dic[++tot].init();
dic[root].word[k] = tot;
}
root = dic[root].word[k];
}
++dic[root].cnt;
}
queue<int>q;
void build(int root) {
while(!q.empty()) q.pop();
q.push(root);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = ; i < ; i++) {
if(dic[u].word[i] == -) continue;
if(u == ) dic[dic[u].word[i]].fail = ;
else {
int v = dic[u].fail;
while(v && dic[v].word[i] == -) v = dic[v].fail;
if(dic[v].word[i] == -) dic[dic[u].word[i]].fail = ;
else dic[dic[u].word[i]].fail = dic[v].word[i];
}
q.push(dic[u].word[i]);
}
}
}
int query(int root,char *s) {
int i,ans = ;
for(i = ; s[i]; i++) {
int k = s[i]-'a';
while(root && dic[root].word[k] == -)
root = dic[root].fail;
if(dic[root].word[k] != -) {
int v = dic[root].word[k];
while(v) {
ans += dic[v].cnt;
v = dic[v].fail;
}
root = dic[root].word[k];
}
}
return ans;
}
char FK[][];
int main() {
int n,m,t;
scanf("%d",&t);
while(t--) {
dic[tot = ].init();
scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i)
scanf("%s",FK[i]);
while(m--) {
scanf("%s",str);
insertWord(,str);
}
build();
for(int i = ; i < n; ++i)
printf("%d\n",query(,FK[i]));
}
return ;
}

这才是AC自动机的正确使用方法,Trie图

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct Trie{
int ch[maxn][],fail[maxn],cnt[maxn],tot;
int newnode(){
memset(ch[tot],,sizeof ch[tot]);
fail[tot] = cnt[tot] = ;
return tot++;
}
void init(){
tot = ;
newnode();
}
void insert(char *str,int root = ){
for(int i = ; str[i]; ++i){
if(!ch[root][str[i]-'a'])
ch[root][str[i]-'a'] = newnode();
root = ch[root][str[i]-'a'];
}
++cnt[root];
}
void build(int root = ){
queue<int>q;
for(int i = ; i < ; ++i)
if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()){
root = q.front();
q.pop();
for(int i = ; i < ; ++i)
if(ch[root][i]){
fail[ch[root][i]] = ch[fail[root]][i];
cnt[ch[root][i]] += cnt[ch[fail[root]][i]];
q.push(ch[root][i]);
}else ch[root][i] = ch[fail[root]][i];
}
}
int query(char *str,int ret = ,int root = ){
for(int i = ; str[i]; ++i){
int x = root = ch[root][str[i]-'a'];
ret += cnt[x];
}
return ret;
}
}ac;
char FK[][],str[];
int main(){
int kase,n,m;
scanf("%d",&kase);
while(kase--){
ac.init();
scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i)
scanf("%s",FK[i]);
while(m--){
scanf("%s",str);
ac.insert(str);
}
ac.build();
for(int i = ; i < n; ++i)
printf("%d\n",ac.query(FK[i]));
}
return ;
}

2015 Multi-University Training Contest 8 hdu 5384 Danganronpa的更多相关文章

  1. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  2. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  3. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  4. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  5. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. Springboot分布式锁实践(redis)

    springboot2本地锁实践一文中提到用Guava Cache实现锁机制,但在集群中就行不通了,所以我们还一般要借助类似Redis.ZooKeeper 之类的中间件实现分布式锁,下面我们将利用自定 ...

  2. 推荐几款VisualStudio的插件

    继前几天推荐了一款转换vs插件的插件后,借着安装VS2013之际,把我比较喜欢的几个插件继续推荐一下. C# Outline 2013 2013 C#的代码折叠最小只能到函数级,不像C++那样可以折叠 ...

  3. Flume 读取实时更新的日志文件

    http://blog.csdn.net/bright60/article/details/50728306 我用了第一种方法. 1. 日志文件每天roate一个新文件 a)  方案一 There i ...

  4. mysql开发之---每日一得01

    2015年7月7日------------------------- 1.truncate表会清空建表语句auto_increment的值:某个表的id即是主键也是自增,你能够选择插入随意id值,假设 ...

  5. Android recovery UI实现分析

    Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...

  6. JavaSE 最easy出错的几个简单的问题

    案例1. package cn.itcast.oop; public class ThisDemo { public static void main(String[] args) { Student ...

  7. Hessian Servlet实例

    Servlet实例 业务场景 在下面的例子中我会发布一个简单的输出字符串的方法,然后在客户端调用并输出结果. 服务器端 环境搭建 在服务端,我们需要引入hessian和servlet的包.编写服务.配 ...

  8. C#.NET编码规范

    一. 环境设置 首先去除开发环境中的一些选项如下: 图一 图二 二. 命名规范 1) 通用性 l 标识的总长度不要超过32个字符. l  标识符的基本语法是以字母和_开始,由字母数字及下划线组成的单词 ...

  9. 维基百科 MediaWiki API 解析

    使用开放的 API 做一个自己的小项目,是一个很好的学习方法.但好像开放的 API 选择并不多.这里给大家多一个选择,简单介绍一下维基百科使用的 MediaWiki API. 简介 先简单介绍几个容易 ...

  10. jQuery分页插件pagination的用法

    https://www.zhangxinxu.com/jq/pagination_zh/ 参数: 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每 ...