您的位置:首页 > 其它

[HDOJ5384]Danganronpa

2015-09-05 11:13 441 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cctype>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <vector>

using namespace std;

const int maxn = 100010;
int n, m;
string a[maxn];
char b[maxn];

typedef struct Node {
Node *next[26];
int cnt;
Node() {
cnt = 0;
for(int i = 0; i < 26; i++) {
next[i] = NULL;
}
}
}Node;

void insert(Node *p, char *str) {
for(int i = 0; str[i]; i++) {
int t = str[i] - 'a';
if(p->next[t] == NULL) {
p->next[t] = new Node();
}
p = p->next[t];
}
p->cnt++;
}

int find(Node *p, string str) {
int res = 0;
for(int i = 0; str[i]; i++) {
int t = str[i] - 'a';
if(p->next[t]) {
p = p->next[t];
}
else {
break;
}
res += p->cnt;
}
return res;
}

void tfree(Node *root) {
for(int i = 0; i < 26; i++) {
if(root->next[i] != NULL) {
tfree(root->next[i]);
}
}
delete root;
}

int solve(Node* root) {
for(int i = 0; i < n; i++) {
int ans = 0;
for(int j = 0; j < a[i].size(); j++) {
ans += find(root, a[i].substr(j));
}
printf("%d\n", ans);
}
}

int main() {
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
Node *root = new Node();
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < m; i++) {
scanf("%s", b);
insert(root, b);
}
solve(root);
tfree(root);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: