hihocoder #1014 trie树
2016-06-13 09:39
295 查看
裸的trie树,无它。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <string> #include <cmath> using namespace std; #define pr(x) cout << #x << ": " << x << " " #define pl(x) cout << #x << ": " << x << endl; class trie { private: int n, m; static const int maxn = 1e5 + 13; char word[maxn]; struct node { int num; node* son[26]; node(void) { num = 0; memset(son, 0, sizeof(son)); } } *root; int ans; public: void input(void) { root = new node; scanf("%d", &n); while (n--) { scanf("%s", word); add(); } scanf("%d", &m); while (m--) { scanf("%s", word); query(); printf("%d\n", ans); } } void add(void) { node *cur = root; for (int i = 0; word[i] != '\0'; i++) { if ((cur -> son)[word[i] - 'a'] == nullptr) (cur -> son)[word[i] - 'a'] = new node; cur = (cur -> son)[word[i] - 'a']; (cur -> num)++; } } void query(void) { node *cur = root; for (int i = 0; word[i] != '\0'; i++) { if (cur -> son[word[i] - 'a'] == nullptr) { ans = 0; break; } cur = cur -> son[word[i] - 'a']; if (!word[i + 1]) { ans = cur -> num; } } } }; int main() { trie temp; temp.input(); return 0; }
相关文章推荐
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- (第22讲)java数组的一些编程题
- POJ_1273_Drainage Ditches(USACO 93)_最大流
- Eclipse安装Freemarker插件
- Netbeans 8.2将支持PHP7 更精彩
- SSL编程(1) 概述
- 一周stackoverflow热门问题选登:如何正确的理解python for循环中的else用法?
- C#简单判断字符编码的方法
- C语言中关于sizeof和strlen的区别