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. httpservlet这个类是属于Tomcat自带jar包的jjava ee类

    bug:The superclass "javax.servlet.http.HttpServlet" was not found on the Java   Build Path ...

  2. HDU 4342

    先确定M的大致范围后即可求. #include <iostream> #include <cstdio> #include <algorithm> #include ...

  3. java中继承关系学习小结

    继承:把多个类中同样的内容提取出来.定义到一个类中,其它类仅仅须要继承该类.就能够使用该类公开的属性和公开的方法.   继承的优点:提高代码的复用性.提高代码的可维护性.让类与类之间产生关系,是多态存 ...

  4. LinkedList 方法知识点

    package test_day_9; import java.util.Iterator; import java.util.LinkedList; public class LinkedListD ...

  5. angularjs1-4 事件指令

    <div ng-app="myApp"> <div ng-controller="firstController"> <div n ...

  6. 指针,c语言的灵魂

    指针是一个值为内存地址的变量. 变量是一块内存空间,指针是变量,是用来存储内存地址的变量. #include <stdio.h> #include <stdlib.h> int ...

  7. 谷歌开源可视化工具Facets,将用于人+AI协作项目研究——无非就是一个用于特征工程探索的绘图工具集,pandas可以做的

    见:http://www.infoq.com/cn/news/2017/07/goole-sight-facets-ai https://github.com/PAIR-code/facets/blo ...

  8. oracle 11gR2 如何修改public ip

    修改public ip 修改前后ip信息 修改前ip信息 p570a 192.168.1.10 p570b 192.168.1.11 修改后ip信息 p570a 1.7.3.112 p570a 1.7 ...

  9. ChildViewController

    View Controller中可以添加多个sub view,在需要的时候显示出来: 可以通过viewController(parent)中可以添加多个child viewController;来控制 ...

  10. IIS之虚拟目录学习

    从刚实习开始就了解到虚拟目录这个词,但是一直没去研究过什么意思,而且也没实际用过.一晃两年过去了,今天正好趁休息,补补脑学习下. 通过百度了解到,虚拟目录创建的目的是为了应对磁盘容量爆满,部署的网站不 ...