Hihocoder 1014 Trie树
2016-12-06 16:58
316 查看
第一次写tire,有一些小trick吧。。。感觉没有以前感觉的难了。
#include <cstdio> #include <string> #include<iostream> #include<vector> #include <stack> #include <queue> #include <map> #include <cstdlib> #include<string.h> #include <cstring> #include <ctime> #include <algorithm> #include <set> using namespace std; typedef long long ll; typedef pair<int, int>pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; typedef vector<vector<ll> >vvi; typedef vector<ll> vi; struct node { int cnt; node *next[26]; node() { cnt = 0; for (int i = 0; i < 26; i++)next[i] = NULL; } }; const int MAXN = 200000 + 50; node head; void insert(char s[]) { node *tmp = &head; for (int i = 0; s[i]; i++) { if (tmp->next[s[i] - 'a'] == NULL)tmp->next[s[i] - 'a'] = new node; tmp = tmp->next[s[i] - 'a']; tmp->cnt++; } } int query(char s[]) { node *tmp = &head; int res = 0; for (int i = 0;s[i]; i++) { if (tmp->next[s[i] - 'a'] == NULL) { res = 0; break; } else { tmp = tmp->next[s[i] - 'a']; res = tmp->cnt; } } return res; } void init() { head.cnt = 0; for (int i = 0; i < 26; i++) head.next[i] = NULL; } int main() { init(); int n, m; scanf("%d", &n); char s[12]; while (n--) { scanf("%s", s); insert(s); } scanf("%d", &m); while (m--) { scanf("%s", s); printf("%d\n", query(s)); } //system("pause"); }
相关文章推荐
- SpringMVC工作原理
- console.log改变样式
- 基于ThinkPHP开发RESTful Web API
- 微信公众号的7个未来
- c#遍历数组
- FastCGI进程意外退出,http错误404.3-NotFound 由于扩展配置问题 dll找不到找定的模块等 dz问题总结
- iframe里不同连接的跳转
- SQL遍历表格数据
- MyEclipse开启Jquery智能提示
- FAQ系列 | Spring框架中调用存储过程失败
- 关于eclipse的国际化插件
- C#中的BackgroundWorker控件
- 常用插件GsonFormat---json数据格式
- CO11N报工
- Android 6.0指纹识别App开发demo
- TCPIP协议详解----网络基础知识
- 微信公众号的八大价值
- nginx + FastDFS分布式文件服务器搭建
- redis中的key设置过期时间
- 在linux上一行代码不用写实现自动采集+hadoop分词