当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- 常用运算符与运算符优先级
- Ffplay document [easy to understand]
- Go language learning tutorial (XV)
- ICTCLAS word Lucene 4.9 binding
- 【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
- Common operators and operator priority
- CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
- 14. Users, groups, and permissions (14)
- Four methods of random number generation | random | math | threadlocalrandom | securityrandom
- Codeforces Round #804 (Div. 2) - A, B, C
猜你喜欢
Build your own website (16)
深度學習 卷積神經網絡(CNN)基礎
Leetcode brush questions: binary tree 18 (largest binary tree)
A solution to PHP's inability to convert strings into JSON
秋招字节面试官问你还有什么问题?其实你已经踩雷了
【数字IC验证快速入门】3、数字IC设计全流程介绍
Parler de threadlocal insecurerandom
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Go language | 03 array, pointer, slice usage
How to select the Block Editor? Impression notes verse, notation, flowus
随机推荐
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
Is it safe for CICC fortune to open an account online?
id选择器和类选择器的区别
港股将迎“最牛十元店“,名创优品能借IPO突围?
CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
Using repositoryprovider to simplify the value passing of parent-child components
Thread pool parameters and reasonable settings
秋招字节面试官问你还有什么问题?其实你已经踩雷了
Go language | 03 array, pointer, slice usage
A solution to PHP's inability to convert strings into JSON
SecureRandom那些事|真伪随机数
leetcode刷题:二叉树13(相同的树)
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
leetcode刷题:二叉树18(最大二叉树)
95后阿里P7晒出工资单:狠补了这个,真香...
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
sun. misc. Base64encoder error reporting solution [easy to understand]
Unity编辑器扩展 UI控件篇
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
Base du réseau neuronal de convolution d'apprentissage profond (CNN)