[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; }
相关文章推荐
- 数据处理——两文件快速对接
- [8-30]文件查找命令locate、find知识梳理
- JAVA学习——多线程
- Just Do It: 学习Sinatra,第一部分
- C#的语句
- Xcode6使用iOS8的sizeClasses解决屏幕适配问题
- win linux 批处理 循环
- 1019. General Palindromic Number (20)
- 使用REXML在ruby中处理xml
- AI (Adobe Illustrator)详细用法(五)
- Linux下常见压缩包的解压方法
- MySQL数据库之布尔类型、枚举类型和集合类型的应用场景详解
- Java类中各成员及函数方法的执行顺序
- 【年总】——因为坚持平凡,所以不平凡
- 二叉树的遍历
- 20150905全志A20上调通rtl8188eus的过程
- 1.每月最后一天的判断
- django 1.8 官方文档翻译: 3-3-3 文件储存API
- POJ_1742 Coins(DP)
- ffmpeg常用基本命令(转)