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. 2015 Multi-University Training Contest 10 hdu 5407 CRB and Candies

    CRB and Candies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. jquery-ajax基础-XMLHttpRequest

    XMLHttpRequest知识点 原生的ajax代码 var xmlhttp; // 声明一个对象 if (window.XMLHttpRequest) {// code for IE7+, Fir ...

  3. webuploader 教程

    1.引入js和css <!-- Web Uploader --> <link rel="stylesheet" type="text/css" ...

  4. HDU 3292

    快速幂模+佩尔方程 #include <iostream> #include <cstdio> #include <algorithm> #include < ...

  5. crm2013使用图片字段

    在CRM2013能够加入图片字段(一个实体仅仅能加入一个图片字段) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2 ...

  6. html5的postmessage实现js前端跨域訪问及调用解决方式

    关于跨域訪问.使用JSONP的方法.我前面已经demo过了.详细见http://supercharles888.blog.51cto.com/609344/856886,HTML5提供了一个很强大的A ...

  7. 转:Oracle GoldenGate学习之Goldengate介绍

    转自:http://blog.sina.com.cn/s/blog_a32eff28010136d9.html 日志或归档日志获得数据的增删改变化,再将这些变化应用到目标数据库,实现源数据库与目标数据 ...

  8. TNS-12555 / TNS-12560 / TNS-00525 Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPR

    TNS-12555 / TNS-12560 / TNS-00525 Error listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPR ...

  9. Blur 算法 (Unity 5.0 Shader)

    一:简单 Blur 算法 一个像素的颜色值由其邻近的若干像素和自己的颜色值的平均值重新定义,以此达到模糊的效果. 如下图,红色的像素点的值将由它和它周围的24个像素的平均值重新定义.计算的范围一般由一 ...

  10. 0x31 质数

    poj2689 算根号R的质数,然后把L~R区间(这个很小啊)的合数判下 #include<cstdio> #include<iostream> #include<cst ...