题目链接:

  Hdu 5384 Danganronpa

题目描述:

  给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少?

解题思路:

  与Hdu 2222  Keywords Search十分相似,hdu2222求目标串中包含几个模式串,本题目求模式串在目标串中出现了几次(比赛的时候模板都不会就悲剧了┭┮﹏┭┮)

  初学AC自动机可以参考大神博客,总结的真的炒鸡棒讷。涨姿势请点击下面尊贵的链接大人:

  AC自动机算法总结            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自动机模板)的更多相关文章

  1. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  2. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  3. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  4. HDU 2222  AC自动机模板题

    1.HDU 2222 2.题意:给出n个单词,一个字串,求有多少个单词在字串里出现了.注意给出的单词可能会重复,重复的不计. 3.总结:入门题.在查询这里还是不太懂. #include<bits ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  7. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  8. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  9. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

随机推荐

  1. Visual studio 2017 中的Javascript智能提示与调试

    1.智能提示 对于JS文件中的API,你若需要让那个JS文件中的方法能够在你写的那个JS文件中能够智能显示的话,直接把它拉进你的JS文件中就好了. 举个例子:你想 在你正在写的a.js文件中引用b.j ...

  2. google官方建议使用的网站性能测试工具

    转自:http://www.laokboke.net/2013/05/12/google-official-recommended-site-performance-testing-tools/ 最近 ...

  3. win8系统 如何默认显示文件扩展名和显示隐藏文件

    装一个魔方软件,然后再任意文件或者文件夹上面右击,依次点击下面两项,就可以默认显示文件扩展名和显示隐藏文件  

  4. spring理解一

    spring基本工作原理例如以下: 1.查找bean配置文件 2.载入bean配置文件并解析生成中间表示BeanDefinition 3.注冊beanDefinition 4.假设是单例或lazy-i ...

  5. I2C上拉电阻取值范围

    I2C总线是微电子通信控制领域中常用的一种总线标准,具备接线少,控制简单,速率高等优点.在I2C电路中常见的上拉电阻有1k.1.5k.2.2k.4.7k.5.1k.10k等等,但是应该如何根据开发要求 ...

  6. [英语学习]王秒同学《21天TED英语精练团》

    第一个分享: Chris Anderson的TED's secret to great public speaking(英音). There's no single formula for a gre ...

  7. 2016/3/27 分页 共X条数据 本页x条 本页从x-y条 x/y页 首页 上一页 123456 下一页 末页 pagego echo $page->fpage(7,6,5,4,3,2,1,0);

    显示效果: fpage.class.php <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; / ...

  8. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  9. IIS application pool access desktop denied

    https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...

  10. bzoj 4521 电话号码

    题目大意: 求$[L,R]$中,满足不同时存在4和8且有连续三个一样的个数 思路: 我为什么要记忆化搜索里带-1啊 我可真是个** 直接记忆化搜索记前两位是否有4,8以及是否满足连续 #include ...