[HDOJ5384]Danganronpa

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384

字典树过,把子弹存入树内,再穷举每一个怪物的子串,看看子串是否在树内存在。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <queue>
 9 #include <map>
10 #include <set>
11 #include <stack>
12 #include <list>
13 #include <vector>
14
15 using namespace std;
16
17 const int maxn = 100010;
18 int n, m;
19 string a[maxn];
20 char b[maxn];
21
22 typedef struct Node {
23     Node *next[26];
24     int cnt;
25     Node() {
26         cnt = 0;
27         for(int i = 0; i < 26; i++) {
28             next[i] = NULL;
29         }
30     }
31 }Node;
32
33 void insert(Node *p, char *str) {
34     for(int i = 0; str[i]; i++) {
35         int t = str[i] - ‘a‘;
36         if(p->next[t] == NULL) {
37             p->next[t] = new Node();
38         }
39         p = p->next[t];
40     }
41     p->cnt++;
42 }
43
44 int find(Node *p, string str) {
45     int res = 0;
46     for(int i = 0; str[i]; i++) {
47         int t = str[i] - ‘a‘;
48         if(p->next[t]) {
49             p = p->next[t];
50         }
51         else {
52             break;
53         }
54         res += p->cnt;
55     }
56     return res;
57 }
58
59 void tfree(Node *root) {
60     for(int i = 0; i < 26; i++) {
61         if(root->next[i] != NULL) {
62             tfree(root->next[i]);
63         }
64     }
65     delete root;
66 }
67
68 int solve(Node* root) {
69     for(int i = 0; i < n; i++) {
70         int ans = 0;
71         for(int j = 0; j < a[i].size(); j++) {
72             ans += find(root, a[i].substr(j));
73         }
74         printf("%d\n", ans);
75     }
76 }
77
78 int main() {
79     // freopen("in", "r", stdin);
80     int T;
81     scanf("%d", &T);
82     while(T--) {
83         Node *root = new Node();
84         scanf("%d %d", &n, &m);
85         for(int i = 0; i < n; i++) {
86             cin >> a[i];
87         }
88         for(int i = 0; i < m; i++) {
89             scanf("%s", b);
90             insert(root, b);
91         }
92         solve(root);
93         tfree(root);
94     }
95     return 0;
96 }
时间: 2024-10-06 22:24:15

[HDOJ5384]Danganronpa的相关文章

HDU 5384 Danganronpa

Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 137 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsof

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

Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 582    Accepted Submission(s): 323 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft

【HDOJ 5384】Danganronpa

[HDOJ 5384]Danganronpa AC自动机...当时感觉用字典树 标神也往自动机想来着..手太生加上时间紧迫也没敲--回来一看题解什么AB同时建自动机...顿时愣了 什么叫同时建= =问了问财神说普通自动机...B串单建 立马疯了--这不就是模板题么... B串建自动机 A串枚举查询 写完兴冲冲1T--立马想法优化 建fail时压缩一下 查询时直接累计 不再循环找fail 171ms...第二个自动机的题..距上次蛮久了 这次一复习 感觉印象差不多有了 代码(模板)如下: #inc

HDU 5384 Danganronpa(AC自动机)

Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 429    Accepted Submission(s): 248 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsof

HDU 5835 Danganronpa(弹丸论破)

HDU 5835 Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 Chisa Yukizome works as a teacher in the school. She prepares many gifts, which consist of n kinds with a[i] quantities of

hdu5384 Danganronpa(AC自动机)

题目: Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 827    Accepted Submission(s): 443 Problem Description Danganronpa is a video game franchise created and developed by Spike Chu

Danganronpa 水题。

Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 304    Accepted Submission(s): 217 Problem Description Chisa Yukizome works as a teacher in the school. She prepares many gifts, which

[2015hdu多校联赛补题]hdu5384 Danganronpa

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f("ababa", "aba")==2   f("ababa", "bab")==1),现在给你一组A串,一组B串,问你对每个A串的是多少 解:ac自动机模板题,可以把A串用'z'+1,连成一个串处理起来比较方便 1 /* 2 * P

HDU 5384 Danganronpa (字典树运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题面: Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 512    Accepted Submission(s): 284 Problem Description Danganronpa is a vid