Hdu 5384 Danganronpa (AC自动机模板)
题目链接:
题目描述:
给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少?
解题思路:
与Hdu 2222 Keywords Search十分相似,hdu2222求目标串中包含几个模式串,本题目求模式串在目标串中出现了几次(比赛的时候模板都不会就悲剧了┭┮﹏┭┮)
初学AC自动机可以参考大神博客,总结的真的炒鸡棒讷。涨姿势请点击下面尊贵的链接大人:
学完AC自动机就可以套模板解决这个站在冰柜上的高冷题目了~~~~.
对所有的Bj建立Trie图,然后在Trie的基础上建立fail数组,在fail数组上每遇到一个Bj的终点,都会对计算结果有1的贡献。
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
const int maxm = ;
char A[maxn][maxm], B[maxm]; struct Trie
{
int next[maxn][], fail[maxn], end[maxn];
int L, root; int newnode ()
{
for (int i=; i<; i++)
next[L][i] = -;
end[L] = ;
return L++;
} void init ()
{
L = ;
root = newnode();
} void insert (char s[])
{
int now = root;
for (int i=; s[i]; i++)
{
if (next[now][s[i]-'a'] == -)
next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
}
end[now] ++;
} void build ()
{
queue <int> Q;
fail[root] = root;
for (int i=; i<; i++)
if (next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while (!Q.empty())
{
int now = Q.front();
Q.pop ();
for (int i=; i<; i++)
{
if (next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push (next[now][i]);
}
}
}
} int query (char s[])
{
int now = root, res = ;
for (int i=; s[i]; i++)
{
now = next[now][s[i] - 'a'];
int temp = now;
while (temp != root)
{
res += end[temp];
temp = fail[temp];
}
}
return res;
}
}ac; int main ()
{
int t, n, m;
scanf ("%d", &t); while (t --)
{
ac.init ();
scanf ("%d %d", &n, &m);
for (int i=; i<n; i++)
scanf ("%s", A[i]); for (int i=; i<m; i++)
{
scanf ("%s", B);
ac.insert (B);
} ac.build ();
for (int i=; i<n; i++)
{
int res = ac.query(A[i]);
printf ("%d\n", res);
} } return ;
}
Hdu 5384 Danganronpa (AC自动机模板)的更多相关文章
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- HDU 2222 AC自动机模板题
1.HDU 2222 2.题意:给出n个单词,一个字串,求有多少个单词在字串里出现了.注意给出的单词可能会重复,重复的不计. 3.总结:入门题.在查询这里还是不太懂. #include<bits ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
随机推荐
- jq自定义裁剪,代码超级简单,易修改
1.自定义宽高效果 1.html 代码 index.html <!DOCTYPE html> <html lang="en"> <head> ...
- 【Todo】STAR面试法
今天在面试培训的时候,接触到了STAR面试法. 觉得挺好的,用来准备非技术面试,还蛮全面的.所以可以多了解一下. 可以参考:http://www.hrloo.com/rz/73652.html
- 【转】Web Worker javascript多线程编程(一)
原文:https://www.cnblogs.com/peakleo/p/6218823.html -------------------------------------------------- ...
- LoadRunner系列之—-02 基于webservice协议的接口测试(脚本实例)
Loadrunner 基于webservice协议的接口压力测试(脚本实例) 接口功能如下:请求接口,报文只有一个参数为证件号码:返回报文中,有证件号码是否能查到对应数据,查到几条数据. 思路:请求w ...
- php 文件压缩zip扩展
<?php function addFileToZip($path, $zip) { $handler = opendir($path); //打开当前文件夹由$path指定. while (( ...
- 微软Build2016:Xamarin杂记
去年的Build2015技术大会.留给人印象最深的莫过是Windows 10在手机端.PC端.Xbox等硬件平台上的大一统.还有非常具有科幻气质的HoloLens技术的各种展示.去年尽管也展示了Xam ...
- iOS开发——高级篇——多线程GCD死锁
面试题 请问以下代码打印结果: - (void)interview01 { // 以下代码是在主线程执行的 NSLog(@"执行任务1"); dispatch_queue_t qu ...
- 浅谈UML学习笔记之用例图
最近一直在学习UML的基础知识,再看完视频之后,并没有很好的总结,在画图的过程中发现了很多的问题,下面是看书的过程自己总结的UML用例图的一点知识,与大家分享一下. 一.概念 用例图是由参与者.用例以 ...
- docker启动centos容器后如何用putty连接
在前面的文章中,我提到过,win10 docker启动容器时,只有配置了宿主机和docker容器的端口映射,外部应用才能访问到容器中的服务,比如映射到Nginx的80端口.现在我将宿主机的某个端口映射 ...
- Iterator 使用
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactor ...