2015 Multi-University Training Contest 8 hdu 5384 Danganronpa
2015-08-14 10:50
399 查看
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输出...
[align=left]Sample Input[/align]
1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
[align=left]Sample Output[/align]
1
1
0
3
7
[align=left]Source[/align]
2015 Multi-University Training Contest 8
解题:AC自动机自动AC
#include <bits/stdc++.h> using namespace std; const int maxn = 250010; struct Trie{ int ch[maxn][26],fail[maxn],cnt[maxn],tot; int newnode(){ memset(ch[tot],0,sizeof ch[tot]); fail[tot] = cnt[tot] = 0; return tot++; } void init(){ tot = 0; newnode(); } void insert(char *str,int root = 0){ for(int i = 0; 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 = 0){ queue<int>q; for(int i = 0; i < 26; ++i) if(ch[root][i]) q.push(ch[root][i]); while(!q.empty()){ root = q.front(); q.pop(); for(int i = 0; i < 26; ++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 = 0,int root = 0){ for(int i = 0; str[i]; ++i){ int x = root = ch[root][str[i]-'a']; ret += cnt[x]; } return ret; } }ac; char FK[100001][10010],str[1000010]; int main(){ int kase,n,m; scanf("%d",&kase); while(kase--){ ac.init(); scanf("%d%d",&n,&m); for(int i = 0; i < n; ++i) scanf("%s",FK[i]); while(m--){ scanf("%s",str); ac.insert(str); } ac.build(); for(int i = 0; i < n; ++i) printf("%d\n",ac.query(FK[i])); } return 0; }
View Code
相关文章推荐
- tools:context=".MainActivity的作用
- 使用Grails快速开发Web应用程序
- 构建您的第一个Grails 应用程序
- 2015 Multi-University Training Contest 1(hdu 5288 - hdu 5299)
- hdu1021Fibonacci Again找规律
- PS和AI软件区别
- HDU - 3394 Railway(连通分量+环)
- qmail 相关问题
- 智能改变未来TurboMail邮件系统招商新计划
- UVA 10986 Sending email 【dijkstra + 堆优化】
- POJ 1363 Rails
- 如何利用OpenCV自带的haar training程序训练分类器
- 使用OGG的logdump工具检查OGG trail file中存在不存在相关表的记录
- pair的用法
- hdu 1702 ACboy needs your help again!
- tail -f 退出不了怎么办
- HDU 1022 Train Problem I
- [CareerCup] 5.4 Explain Expression ((n & (n-1)) == 0) 解释表达式
- 2015 Multi-University Training Contest 8 hdu 5389 Zero Escape
- HDU 4720 Naive and Silly Muggles