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. BZOJ2693:JZPTAP——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2693 Description   Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 ...

  2. IP协议简介

    一.IP 1.IP是TCP/IP协议簇中最为核心的协议,所有的TCP.UDP.ICMP及IGMP数据都是以IP数据报格式传输. 2.IP提供不可靠.无连接的数据报传送服务 (1)不可靠:不保证IP数据 ...

  3. BZOJ1034 [ZJOI2008]泡泡堂BNB 【贪心】

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3531  Solved: 1798 [Submit][ ...

  4. LOJ 模拟赛

    1.LOJ 507 接竹竿 link dp[i]表示前i个的最大分数,所以dp[i]=max(dp[i-1],dp[j-1]+sum[i]-sum[j-1])   (color i ==color j ...

  5. 自动清理N天前的二进制日志

    这里以自动清理5天前的二进制日志为例(做了同步或依赖于二进制日志备份的请慎用): 以root身份登录数据库,执行以下命令: ; 首次设置expire_logs_days参数后需要执行flush log ...

  6. Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)

    C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...

  7. bzoj 3246 [Ioi2013]Dreaming 贪心

    [Ioi2013]Dreaming Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 638  Solved: 241[Submit][Status][Di ...

  8. http-反向代理学习

    主要是学习了反向代理. 结合公司的方向代理使用,然后与同事进行交流,知识还是需要通过交流才能印象深刻,以后多多交流.

  9. 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]

    骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...

  10. 【BZOJ4821】【SDOI2017】相关分析 [线段树]

    相关分析 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Frank对天文学非常感兴趣,他经 ...