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 ...
随机推荐
- datasnap使用ipv6
有些人说DATASNAP不支持IPv6,只支持IPv4. 这是不正确的. DATASNAP默认是使用IPv4在ipv6 环境下 怎样用datasnap?Params.Values['Communica ...
- FIREDAC驱动ORACLE的配置
1)部署中间件所在的机器必须安装OCI 2)verdorlib,指定OCI所在路径
- iOS: 解决Asset Catalog Compile Error - TDDIstiller instance can only be distilled only one time的错误
执行命令:rm -rf /Users/<用户名>/Library/Developer/Xcode/DerivedData 然后重新编译项目即可.
- ArcGIS Server启动服务报:ERROR: Unable to start Xvfb on any port in the range 6600 - 6619
http://blog.csdn.net/linghe301/article/details/10094421 今天尝试在Linux环境下安装ArcGIS Server10.2,启动服务碰到一个错误: ...
- Feign简介
Feign简介
- Android c/c++ 应用向linux 平台迁移执行
鉴于近期在minicamera (linux + FPGA) 平台上模拟 安卓系统camera流程遭遇不断升级同步的困扰.尤其是 不开放源代码后 , 应用要链接封闭动态库造成的困难.以及在sprdis ...
- apache下配置认证用户
有时候我们须要给我apacheserver下制定的文件夹加上用户认证,方便一些而用户进行文件的浏览.配置例如以下: 1 设置用户 1 htpasswd -c file_path user_name 回 ...
- nyoj473 A^B Problem (高速幂)
题目473 题目信息 执行结果 pid=473" style="text-decoration:none; color:rgb(55,119,188)">本题排行 ...
- 利用wxpython编写GUI
首先建立一个简单的布局合理的图形界面,但是其中按下按键没有事情发生 #encoding=utf-8 __author__ = 'heng' #编写一个GUI import wx app = wx.Ap ...
- 浏览器同部署了https的服务器交互的过程
1 浏览器发起https请求 2 https服务器发送自己的公钥给浏览器 3 浏览器用https服务器发送过来的公钥加密一个用于双方通信的的对称密码 4 https服务器用自己的私钥解密,获取对称密码 ...