hdu 5384 Danganronpa
Posted 8023spz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5384 Danganronpa相关的知识,希望对你有一定的参考价值。
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 A
i![技术分享图片]()
, and bullets are some strings B
j![技术分享图片]()
. The damage to verbal evidence A
i![技术分享图片]()
from the bullet B
j![技术分享图片]()
is f(A
i
,B
j
)
.
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 A
i![技术分享图片]()
after shooting all m
bullets B
j![技术分享图片]()
, in other words is ∑
m
j=1
f(A
i
,B
j
)
.
Now, Stilwell is playing this game. There are n
Verbal evidences will be described as some strings A
f(A,B)=∑
i=1
|A|−|B|+1
[ A[i...i+|B|−1]=B ]![技术分享图片]()
In other words, f(A,B)For example: f(ababa,ab)=2
Stilwell wants to calculate the total damage of each verbal evidence A
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 A
i![技术分享图片]()
, describing a verbal evidence.
Next m
lines, each line contains a string B
j![技术分享图片]()
, describing a bullet.
T≤10
For each test case, n,m≤10
5![技术分享图片]()
, 1≤|A
i
|,|B
j
|≤10
4![技术分享图片]()
, ∑|A
i
|≤10
5![技术分享图片]()
, ∑|B
j
|≤10
5![技术分享图片]()
For all test case, ∑|A
i
|≤6∗10
5![技术分享图片]()
, ∑|B
j
|≤6∗10
5![技术分享图片]()
, A
i![技术分享图片]()
and B
j![技术分享图片]()
consist of only lowercase English letters
For each test case, the first line contains two integers n
Next n
Next m
T≤10
For each test case, n,m≤10
For all test case, ∑|A
Output
For each test case, output n
lines, each line contains a integer describing the total damage of A
i![技术分享图片]()
from all m
bullets, ∑
m
j=1
f(A
i
,B
j
)
.
Sample Input
1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
Sample Output
1
1
0
3
7
Author
SXYZ
Source
Recommend
给出n个A串和m个B串,对于每个A串,输出里面出现了几次B串,ac自动机对所有B串构建字典树,然后进行匹配查找。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAX 100001 using namespace std; int pos; int trie[MAX][26],fail[MAX] = {-1},sum[MAX]; char s[100001][10001]; void Insert_Str(char *s) {///字符串插入到字典树中 int i = -1,r = 0; while(s[++ i]) { int d = s[i] - ‘a‘; if(!trie[r][d]) { trie[r][d] = ++ pos; } r = trie[r][d]; } sum[r] ++; } void Build_Fail() {///通过父结点的Fail更新子结点的Fail queue<int> q; q.push(0); while(!q.empty()) { int id = q.front(); q.pop(); for(int i = 0;i < 26;i ++) { if(trie[id][i]) {///第i个儿子存在 int temp = fail[id];///temp赋值当前节点的Fail while(temp != -1) { if(trie[temp][i]) { fail[trie[id][i]] = trie[temp][i]; break; } temp = fail[temp]; } q.push(trie[id][i]); } } } } void Ac_automation(int k) { int i = -1,r = 0,ans = 0; while(s[k][++ i]) { int d = s[k][i] - ‘a‘; while(r && !trie[r][d]) r = fail[r];///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点 if(trie[r][d]) r = trie[r][d]; int temp = r; while(temp) { ans += sum[temp]; temp = fail[temp];///找最长后缀 } } printf("%d ",ans); } int main() { char str[10001]; int t,n,m; scanf("%d",&t); while(t --) { pos = 0; memset(trie,0,sizeof(trie)); memset(fail + 1,0,sizeof(fail)); memset(sum,0,sizeof(sum)); scanf("%d %d",&n,&m); for(int i = 0;i < n;i ++) { scanf("%s",s[i]); } for(int i = 0;i < m;i ++) { scanf("%s",str); Insert_Str(str); } Build_Fail(); for(int i = 0;i < n;i ++) { Ac_automation(i); } } return 0; }
以上是关于hdu 5384 Danganronpa的主要内容,如果未能解决你的问题,请参考以下文章