您的位置:首页 > 其它

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: