当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- Leetcode brush questions: binary tree 11 (balanced binary tree)
- Oracle-表空间管理
- c——顺序结构
- sun.misc.BASE64Encoder报错解决方法[通俗易懂]
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- 微信小程序正则表达式提取链接
- 深度學習 卷積神經網絡(CNN)基礎
- Elk distributed log analysis system deployment (Huawei cloud)
- 2020 CCPC 威海 - A. Golden Spirit(思维),D. ABC Conjecture(大数分解 / 思维)
- Go language | 03 array, pointer, slice usage
猜你喜欢
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
leetcode刷题:二叉树14(左叶子之和)
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
.Net分布式事務及落地解决方案
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
Unity编辑器扩展 UI控件篇
IC科普文:ECO的那些事儿
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
95后阿里P7晒出工资单:狠补了这个,真香...
随机推荐
C - sequential structure
js方法传Long类型id值时会出现精确损失
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
How to select the Block Editor? Impression notes verse, notation, flowus
Build your own website (16)
Database logic processing function
通配符选择器
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
BZOJ 3747 POI2015 Kinoman 段树
Is it safe for Galaxy Securities to open an account online?
ICTCLAS用的字Lucene4.9捆绑
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Leetcode skimming: binary tree 16 (path sum)
id选择器和类选择器的区别
挖财钱堂教育靠谱安全吗?
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
建立自己的网站(16)
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Where is the operation of new bonds? Is it safer and more reliable to open an account
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验