2015 Multi-University Training Contest 8 hdu 5384 Danganronpa
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输出...
#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的更多相关文章
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- 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 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- VirtualBox扩展包安装教程|VirtualBox扩展增强包怎么安装
VirtualBox是一款功能强大的免费虚拟机软件,一般我们安装VirtualBox后要安装扩展增强包,VirtualBox扩展包包含USB2.0和USB3.0控制等支持功能,如果没有装,在使用过程中 ...
- NYOJ_94 cigarettes 递归VS迭代
题目地址 分析: 英文题事实上看懂意思和正常的也都差点儿相同.就算有几个单词不认识也无伤大雅. 一共同拥有n支烟,每天抽k支. 每抽完k支,会得到一仅仅. a组数据. 输入n k的个数.输出一共抽了 ...
- Android——隐藏输入法的小技巧
今天偶然在百度地图提供的DEMO里看到这样一段代码.认为确实是个小技巧,就写下来分享一下. 针对的问题: 我们在开发android界面的时候,常常使用EditText控件.然后每次进入这个页面的时候, ...
- zzulioj--1637--Happy Thanksgiving Day - WoW yjj!(水)
1637: Happy Thanksgiving Day - WoW yjj! Time Limit: 1 Sec Memory Limit: 128 MB Submit: 104 Solved: ...
- [SCOI 2009] 生日快乐
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1024 [算法] 直接DFS,即可 [代码] #include<bits/std ...
- netty结构
一.先纵览一下Netty,看看Netty都有哪些组件? 为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架构中是怎么协调工作的.Netty应用中必不可 ...
- thinkPHP5 报错session_start(): No session id returned by function解决方法
这是因为用Redis接管了session状态储存,但是Redis又连接不正常导致的 在服务器上查看Redis运行状态一切正常,set.get也没有问题,最后琢磨了半天才发现是PHPRedis扩展没有安 ...
- CSS中alt和title属性的正确使用
1.在<img>标签中的使用 alt:全称为alttext,实质是当图片无法正确显示时用于替换(在IE下同时起到了title的作用,即鼠标滑过时文字提示): title:鼠标经过时文字提示 ...
- 40.DOM读取XML
main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...
- QT-项目文件说明
前言:如题. 一.项目文件概述 文件 功能 helloworld.pro 包含了项目信息 helloworld.pro.user 用户信息 hellodialog.h 自定义类hellodialog的 ...