当前位置:网站首页>信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
2022-07-05 20:10:00 【君义_noip】
【题目链接】
ybt 1337:【例3-2】单词查找树
洛谷 P5755 [NOI2000] 单词查找树
【题目考点】
1. 字典树
【解题思路】
解法1:字典树
字典树模板题。学习字典树相关知识请在csdn搜“字典树”或“tire树”。
该题说文件总长度不超过32K, 32 K B = 32 ∗ 1024 B = 32768 B 32KB = 32*1024B = 32768B 32KB=32∗1024B=32768B,总字符个数不会超过33000个。因此结点池总数量设为33000。
【题解代码】
解法1:字典树
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int child[26];//child[i]:字符是i+'A'的孩子结点的地址
};
Node node[33000];
int p, root;//根结点root地址为0,从地址1开始分配结点
void insert(string s)//向字典树插入单词s
{
int pf = root;//当前关注的结点的地址
for(int i = 0; i < s.length(); ++i)
{
if(node[pf].child[s[i]-'A'] == 0)
node[pf].child[s[i]-'A'] = ++p;//分配新结点
pf = node[pf].child[s[i]-'A'];//pf指向下一个结点
}
}
int getNodeNum(int r)//获取根结点地址为r的树的结点数
{
int sum = 1;//自己就是1个结点
for(int i = 0; i < 26; ++i)//统计r子树结点数量
{
if(node[r].child[i] > 0)
sum += getNodeNum(node[r].child[i]);
}
return sum;
}
int main()
{
string s;
while(cin >> s)
insert(s);//将单词s插入字典树
cout << getNodeNum(root);//获取整棵树的结点数
return 0;
}
边栏推荐
- 常用运算符与运算符优先级
- DP:树DP
- B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
- kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
- Debezium series: parsing the default value character set
- Build your own website (16)
- Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
- CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
- IC科普文:ECO的那些事儿
- c語言oj得pe,ACM入門之OJ~
猜你喜欢
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
Android interview classic, 2022 Android interview written examination summary
计算lnx的一种方式
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Go language | 01 wsl+vscode environment construction pit avoidance Guide
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
A solution to PHP's inability to convert strings into JSON
leetcode刷题:二叉树14(左叶子之和)
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
随机推荐
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
.Net分布式事务及落地解决方案
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
95后阿里P7晒出工资单:狠补了这个,真香...
Redis cluster simulated message queue
c語言oj得pe,ACM入門之OJ~
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
常用运算符与运算符优先级
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
Leetcode brush question: binary tree 14 (sum of left leaves)
Bzoj 3747 poi2015 kinoman segment tree
How to select the Block Editor? Impression notes verse, notation, flowus
国信证券在网上开户安全吗?
js方法传Long类型id值时会出现精确损失
[C language] merge sort
ffplay文档[通俗易懂]
线程池参数及合理设置
No matter how busy you are, you can't forget safety
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design