当前位置:网站首页>信息学奥赛一本通 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刷题:二叉树11(平衡二叉树)
- 1:引文;
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
- No matter how busy you are, you can't forget safety
- 淺淺的談一下ThreadLocalInsecureRandom
- 线程池参数及合理设置
- Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
- Concept and syntax of function
- kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
猜你喜欢

解决php无法将string转换为json的办法

How to safely and quickly migrate from CentOS to openeuler

Go language | 01 wsl+vscode environment construction pit avoidance Guide

Unity编辑器扩展 UI控件篇

Jvmrandom cannot set seeds | problem tracing | source code tracing

Using repositoryprovider to simplify the value passing of parent-child components

Zero cloud new UI design

- Oui. Net Distributed Transaction and Landing Solution

Securerandom things | true and false random numbers

.Net分布式事務及落地解决方案
随机推荐
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
Multi branch structure
Tasks in GStreamer
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
Wildcard selector
Some problems encountered in cocos2d-x project summary
点云文件的.dat文件读取保存
sun. misc. Base64encoder error reporting solution [easy to understand]
图嵌入Graph embedding学习笔记
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
ffplay文档[通俗易懂]
实操演示:产研团队如何高效构建需求工作流?
Analysis of openh264 decoded data flow
Go language | 03 array, pointer, slice usage
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
leetcode刷题:二叉树15(找树左下角的值)
Cocos2d-x项目总结中的一些遇到的问题
Debezium series: modify the source code to support drop foreign key if exists FK
Process file and directory names
挖财钱堂教育靠谱安全吗?