【hiho一下第二周 】Trie树
2017-10-04 18:44
357 查看
【题目链接】:http://hihocoder.com/problemset/problem/1014
【题意】
【题解】
在字典树的域里面加一个信息cnt;
表示这个节点下面,记录有多少个单词;
在找的时候,直接到那个节点;
然后输出这个cnt就好了
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 100e4+100; struct node { int child[27],cnt; }; node tree ; int n,root = 1,tot = 1; char s[15]; int main() { //freopen("F:\\rush.txt","r",stdin); ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use cin >> n; rep1(i,1,n) { cin >> s; int len = strlen(s); int now = root; rep1(j,0,len-1) { int t = s[j]-'a'+1; if (tree[now].child[t]==0) tree[now].child[t]=++tot; now = tree[now].child[t]; tree[now].cnt++; } } cin >> n; rep1(i,1,n) { cin >> s; int len = strlen(s); int now = root; rep1(j,0,len-1) { int t = s[j]-'a'+1; now = tree[now].child[t]; } cout << tree[now].cnt << endl; } return 0; }
相关文章推荐
- 编程之美hiho一下第二周Trie树
- hiho一下第二周Trie树 题解
- hiho一下 第二周&第四周:从Trie树到Trie图
- hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)
- (hiho一下第二周)#1014 Trie树 【模版】
- hiho一下 第二周 trie树
- 【hiho一下】第二周 Trie树
- hiho一下第二周#1014 : Trie树
- hiho一下第二周 Trie树
- hiho一下第二周 Trie树
- hiho一下第二周:Trie树
- hiho一下第二周 Hihocoder #1014 : Trie树
- HIHO第二周-Trie树
- hiho第二周——Trie树
- hiho 第二周 trie树
- hiho一下第二周——字典树
- 简单的字典树实现——hiho一下第二周
- hiho一下 第二周
- Trie树 hiho一下第2周
- Trie树的两种实现 - hiho一下