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】的微服务【Jenkins】自动化部署

    最近,也是抽空整理了一些在工作中积累的经验,通过博客记录下来分享给大家,希望能对大家有所帮助: 一.关于自动化部署 关于自动化部署的优点,我就不在这里赘述了:只要想想手工打包.上传.部署.重启的种种, ...

  2. SQL--各种约束

    约束名称 含义 主键约束 定义一个唯一的标识符 外键约束 为了维护和主键表的数据完整性 check约束 限定表中某个列的值的范围 default约束 如果没有指定插入值,则插入默认值 unique约束 ...

  3. Ubuntu12.04 下 GTK3.xx 的安装、编译和測试

    用此方法成功在UBUNTU 12.04下安装GTK 3.xxx. 一.安装 1.安装gcc/g++/gdb/make 等基本编程工具 $sudo apt-get install build-essen ...

  4. cocos2D(六)----CCLayer

    一个游戏中能够有非常多个场景,每一个场景里面又可能包括有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身差点儿没什么功能.对照CCNode,CCLayer可用于接收触摸和加速计输入. ...

  5. angularjs1-8,cacheFactory,sce

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  6. NOI.AC: NOIP2018 全国模拟赛习题练习

    闲谈: 最后一个星期还是不浪了,做一下模拟赛(还是有点小虚) #30.candy 题目: 有一个人想买糖吃,有两家商店A,B,A商店中第i个糖果的愉悦度为Ai,B商店中第i个糖果的愉悦度为Bi 给出n ...

  7. C# First and FirstOrDefault 方法详解

    在工作中我们经常会遇到有关LINQ 的一些问题.这时我们就用到lambda 表达式. 下面是我在工作遇到的. First  and FirstOrDefault  这两方法.我今天把它记录一下. 需要 ...

  8. 一个Python项目的创建架构

    要进行Python项目的编写,很多人刚开始一筹莫展,不知道该如何去构建一个项目,现在粗略的描述一下一个项目的创建过程,供大家参考了解一下: 大家可以先忽略其中创建的函数 ,每个包的含义都有定义,大家可 ...

  9. ROS-Solidworks转URDF

    前言:URDF建模很粗糙,而ros提供了支持sw转urdf的插件,可以使建模更精细化. 一.安装sw_urdf_exporter插件 sw_urdf_exporter插件网址:http://wiki. ...

  10. Java对象、Json、Xml转换工具Jackson使用

    在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...