当前位置:网站首页>Dictionary tree template
Dictionary tree template
2022-04-22 05:48:00 【lxt1101】
Ignatius I've had a problem recently , The teacher gave him a lot of words ( Only lowercase letters , There will be no repetition of words ), Now the teacher asked him to count the number of words prefixed with a string ( Words themselves are prefixes of their own ).
Input
The first part of the input data is a word list , One word per line , The length of a word does not exceed 10, They represent what teachers give to Ignatius Statistical words , An empty line represents the end of the word list . The second part is a series of questions , One question per line , Every question is a string .
Be careful : There is only one set of test data , Processing to the end of the file .
Output
For each question , Give the number of words prefixed with the string .
Sample
| Inputcopy | Outputcopy |
|---|---|
banana band bee absolute acm ba b band abc |
2 3 1 0 |
#include<iostream>
#include<math.h>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = 1e6 + 10;
int tree[maxn][30];
ll sum[maxn];
bool flag[maxn];
int tot;
void build_tree(char* s) {
int root = 0;
int id;
int len = strlen(s);
for (int i = 0; i < len; i++) {
id = s[i] - 'a';
if (!tree[root][id])tree[root][id] = ++tot;
sum[tree[root][id]]++;// Every time a word comes here, add one , You can know how many words there are after
root = tree[root][id];
}
flag[root] = true;
}
ll find(char* s) {
int root = 0;
int id;
ll res = 0;
int len = strlen(s);
for (int i = 0; i < len; i++) {
id = s[i] - 'a';
if (!tree[root][id])return 0;
root = tree[root][id];
}
return sum[root];
}
char s[maxn];
int main() {
while (gets(s)) {
if (s[0] == '\0')break;
build_tree(s);
}
while (cin >> s) {
cout << find(s) << endl;
}
return 0;
}
版权声明
本文为[lxt1101]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220535140390.html
边栏推荐
- torch 循环神经网络torch.nn.RNN()和 torch.nn.RNNCell()
- MySQL command
- LeetCode 514. 自由之路--动态规划
- Error Putty X11 proxy: Authorisation not recognised
- Fastjson determines whether the JSON string is object or list < object >
- Advanced part of MySQL
- C语言--经典100题
- 寻找矩阵中“块“的个数(BFS)
- Pseudo code block writing (for paper writing)
- 数据挖掘——朴素贝叶斯分类
猜你喜欢
随机推荐
AcWing 1096. 地牢大师(三维 BFS)代码详细注释
Force buckle 876 Intermediate node of linked list
raspberry keras-ocr can‘t allocate memory in static TLS block
简单dp问题-母牛繁殖和超级爬楼梯
7-10 最长对称子串 (25 分)(暴力题解)C语言
数据挖掘——认识数据
关于form表单点击submit按钮后,页面自动刷新的问题解决
Eight queens problem (backtracking method, solving N Queens at the same time)
矩阵乘法实现
uniapp:HBuilderX运行uniapp项目到夜神模拟器
九宫幻方-第八届蓝桥省赛-C组(DFS和对比所有幻方种类)
根源:pip终端下载的包import不能用
Simple DP questions - cow breeding and super stair climbing
SQL面试题总结(更新中)
LeetCode 2049. 统计最高分的节点数目--树的遍历
TCGA下载GBM患者的RNA-seq数据
MySQL basic commands and exercises (I)
excel的相对引用和绝对引用
Exploration des données - regroupement
JUC concurrent programming








