hiho一下第二周 Trie树
2015-10-06 23:30
423 查看
题目链接:http://hihocoder.com/problemset/problem/1014
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 1e6 + 5; // 1e5 * 10 (1e5个单词 * 单词长度 (<= 10)) const int N = 26; int le; char str ; struct Trie { int cnt; int next ; void init() { cnt = 0; memset(next, -1, sizeof(next)); } }T[maxn]; void Build(char *s) { int i = 0, p = 0; while (s[i]) { int x = s[i] - 'a'; if (T[p].next[x] == -1) { T[le].init(); T[p].next[x] = le++; // printf("T[%d].next[%d] = %d, ", p, x, T[p].next[x]); } p = T[p].next[x]; T[p].cnt++; // printf("T[%d].cnt = %d\n", p, T[p].cnt); i++; } } void Query(char *s) { int i = 0, p = 0; while (s[i]) { int x = s[i] - 'a'; if (T[p].next[x] == -1) { puts("0"); return ; } p = T[p].next[x]; i++; } printf("%d\n", T[p].cnt); } int main() { int n, m; #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF) { le = 1; T[0].init(); // !root while (n--) { scanf("%s", str); Build(str); } scanf("%d", &m); while (m--) { scanf("%s", str); Query(str); } } return 0; }
相关文章推荐
- frame | center |bounds |transform
- ubuntu下Android开发环境的搭建:eclipse+SDK详细安装教程+常见问题及其解决方案
- 结对项目
- UVA10123木板上放石头使木板平衡,递归加强剪枝
- leetcode题目 跳跃游戏系列题目
- HDU3966【树链剖分】。
- 提高Android Studio中Gradle执行效率
- openstack手动安装
- 一些安卓开源框架整理
- 屏蔽浏览器窗口自带的事件
- linq to object 未完待续
- loadView、viewDidLoad及viewDidUnload的关系
- VB 连接AutoCad 模块代码
- 结对项目博客
- ubuntu装jdk
- Tomcat连接池的配置
- Java web 图片上传(文件上传)
- 从今天开始,每天写一篇博客
- 个人博客作业_week3
- jquery easyui DataGrid