Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 210    Accepted Submission(s): 116

Problem Description
Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation" (ronpa).

Now, Stilwell is playing this game. There are n verbal evidences, and Stilwell has m "bullets". Stilwell will use these bullets to shoot every verbal evidence.

Verbal evidences will be described as some strings Ai, and bullets are some strings Bj. The damage to verbal evidence Ai from the bullet Bj is f(Ai,Bj).

f(A,B)=∑i=1|A|−|B|+1[ A[i...i+|B|−1]=B ]

In other words, f(A,B) is equal to the times that string B appears as a substring in string A.
For example: f(ababa,ab)=2, f(ccccc,cc)=4

Stilwell wants to calculate the total damage of each verbal evidence Ai after shooting all m bullets Bj, in other words is ∑mj=1f(Ai,Bj).

 
Input
The first line of the input contains a single number T, the number of test cases.
For each test case, the first line contains two integers n, m.
Next n lines, each line contains a string Ai, describing a verbal evidence.
Next m lines, each line contains a string Bj, describing a bullet.

T≤10
For each test case, n,m≤105, 1≤|Ai|,|Bj|≤104, ∑|Ai|≤105, ∑|Bj|≤105
For all test case, ∑|Ai|≤6∗105, ∑|Bj|≤6∗105, Ai and Bj consist of only lowercase English letters

 
Output
For each test case, output n lines, each line contains a integer describing the total damage of Ai from all m bullets, ∑mj=1f(Ai,Bj).
 
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自动机 kuangbin模版
**/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#define maxn 500010
using namespace std;
struct Tire
{
int next[maxn][], fail[maxn], end[maxn];
int root, L;
int newnode() {
for(int i = ; i < ; i++) {
next[L][i] = -;
}
end[L++] = ;
return L - ;
}
void init() {
L = ;
root = newnode();
}
void insert(char s[]) {
int len = strlen(s);
int now = root;
for(int i = ; i < len; 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 buf[]) {
int len = strlen(buf);
int now = root;
int res = ;
for(int i = ; i < len; i++) {
now = next[now][buf[i] - 'a'];
int temp = now;
while(temp != root) {
res += end[temp];
temp = fail[temp];
}
}
return res;
}
};
char buf[][];
char buf1[];
Tire ac;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++)
{
scanf("%s", buf[i]);
}
ac.init();
for(int i = ; i < m; i++)
{
scanf("%s", buf1);
ac.insert(buf1);
}
ac.build();
for(int i = ; i < n; i++)
{
printf("%d\n", ac.query(buf[i]));
}
}
return ;
}

HDU-5384的更多相关文章

  1. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  2. hdu 5384 Danganronpa

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 思路:没学自动机时以为是道KMP然后就tle了好几把,AC自动机模板题 #include<cs ...

  3. HDU 5384 AC自动机

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...

  4. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  5. HDU 5384 Danganronpa (Trie树)

    题意:给出两个集合S和T,集合中每个元素是个字符串,而T集合中任一元素都是个子弹,可以打S中的任一怪物,如果子弹是怪物的子串,那么才有伤害值1,若在怪物中出现多次,次数为该子弹打到该怪物的伤害值.每个 ...

  6. hdu 5384 Danganronpa(字典树)

    题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...

  7. HDU 5384——Danganronpa——————【AC自动机】

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  8. HDU 5384 Danganronpa (2015年多校比赛第8场)

    1.题目描写叙述:点击打开链接 2.解题思路:本题利用字典树解决.本题要求查找全部的B[j]在A[i]中出现的总次数.那么我们能够建立一颗字典树,将全部的B[j]插入字典树,因为一个串的全部字串相当于 ...

  9. 【HDU 5384】Danganronpa(AC自己主动机)

    看官方题解貌似就是个自己主动机裸题 比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢.. 存下模板吧 #include<cstdio> #include&l ...

  10. 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

随机推荐

  1. 有序数列第K小

    有序数列第K小 题目描述 给出两个长度分别为\(n,m\)的单调非递减数列,求出它们合并后的第\(k\)小值. 输入输出格式 输入格式: 第一行三个数,\(n,m,k\)如题意所述: 第二行\(n\) ...

  2. android 应用使用Root权限执行linux命令

    要让Android应用使用Root权限,首先Android设备必须已经获得Root权限.之后可以通过下面的代码取得process对象. Process process = Runtime.getRun ...

  3. Eclipse debug模式下使用16进制(Hex)查看变量值

    转载自:http://blog.csdn.net/jinhill/article/details/5767035 因为工作中需要查看byte[]原始编码,eclipse中debug模式下默认查看byt ...

  4. linux上抓包

    使用tcpdump命令. 使用tcpdump -help查看其用法. -i eth0:在第一块网卡上进行抓包. -w filename.cap:将抓的保存到当前目录下的filename.cap文件中, ...

  5. 在linux下创建软链接(即目录映射)

    在linux中创建软链接,使用命令:ln -s. 语法:ln -s 源文件 目标文件.

  6. 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

    https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...

  7. 解决gitlab关闭登录选项问题

    1.连接资料库      mysql -uroot -p   2.use gitlabhq_production; 3.进入后,输入下面语句UPDATE application_settings se ...

  8. 在ubuntu16.04+python3.5情况下安装nltk,以及gensim时pip3安装不成功的解决办法

    在ubuntu16.04+python3.5情况下安装nltk,以及gensim时pip3安装不成功的解决办法,我刚开始因为不太会用linux命令,所以一直依赖于python 的pip命令,可是怎么都 ...

  9. Part2-HttpClient官方教程-Chapter3-HTTP状态管理

    ps:近日忙于课设与一个赛事的准备....时间真紧啊~~ 最初,HTTP被设计为一种无状态的,面向请求/响应的协议,它并没有为跨越多个逻辑相关的请求/响应交换的有状态会话做出特殊规定.随着HTTP协议 ...

  10. 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week2 Neural Networks Basics课堂笔记

    Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week2 Neural Networks Basics 2.1 ...