您的位置:首页 > 大数据 > 人工智能

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: