【Trie】查单词(C++)

本文介绍了如何使用C++实现Trie字典树,以便在英语四级考试作弊场景下快速查找单词。题目提供了一组单词及其在字典中的页码,要求根据输入的查询单词返回其对应的页码。输入包括单词数量、每个单词及其页码、查询单词的数量和查询单词本身。输出为每个查询单词对应的页码。

[Submit][Status][Web Board]

Description

全国英语四级考试就这样如期到来了,可是小Y依然没有做好充分准备。为了能大学毕业,可怜的小Y准备作弊。
小Y费尽心机,在考试的时候夹带了一本字典进考场。现在的问题是:考试的时候可能有很多单词要查,小Y能不能来得及呢?

Input

第一行一个整数N,表示字典中一共有多少个单词。
接下来每两行表示一个单词,其中:第一行是一个长度≤100的字符串,表示这个单词,全部是小写字母,单词不会重复;第二行是一个整数,表示这个单词在字典中的页码。
接下来一行是一个整数M,表示要查的单词数。
接下来M行,每行一个字符串,表示要查的单词,保证在字典中存在。
N≤10000,M≤10000

Output

M行,每行一个整数,表示第i个单词在字典中的页码。

Sample Input

2
scan
10
word
15
2
scan
word

Sample Output

10
15

HINT

[Submit][Status]

trie【字典树】模板题,直接看代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
char str[110];
int a
### C++ 实现 Trie 树的数据结构 Trie 树(字典树或前缀树)是一种高效的字符串处理数据结构,广泛应用于自动补全、拼写检等领域。以下是基于已有引用内容和专业知识构建的一个完整的 C++ 示例代码。 #### 完整的 C++ 实现示例 以下是一个标准的 Trie 树实现,支持插入、找和前缀匹配功能: ```cpp #include <iostream> #include <unordered_map> #include <vector> class TrieNode { public: bool isEndOfWord; std::unordered_map<char, TrieNode*> children; TrieNode() : isEndOfWord(false) {} }; class Trie { private: TrieNode* root; void deleteHelper(TrieNode* node) { for (auto& child : node->children) { deleteHelper(child.second); } delete node; } public: Trie() : root(new TrieNode()) {} ~Trie() { deleteHelper(root); } void insert(const std::string& word) { TrieNode* current = root; for (char c : word) { if (current->children.find(c) == current->children.end()) { current->children[c] = new TrieNode(); } current = current->children[c]; } current->isEndOfWord = true; } bool search(const std::string& word) const { TrieNode* current = root; for (char c : word) { if (current->children.find(c) == current->children.end()) { return false; } current = current->children.at(c); } return current->isEndOfWord; } bool startsWith(const std::string& prefix) const { TrieNode* current = root; for (char c : prefix) { if (current->children.find(c) == current->children.end()) { return false; } current = current->children.at(c); } return true; } }; ``` 上述代码实现了 Trie 的核心功能:`insert` 方法用于向树中插入单词;`search` 方法判断某个单词是否存在;`startsWith` 方法则可以用来检测特定前缀的存在性[^2]。 --- #### 使用示例 下面是如何使用上面定义的 `Trie` 类来完成一些常见操作的例子: ```cpp int main() { Trie trie; // 插入几个单词 trie.insert("apple"); trie.insert("app"); // 搜索单词 std::cout << (trie.search("apple") ? "Found" : "Not Found") << std::endl; // 输出: Found std::cout << (trie.search("app") ? "Found" : "Not Found") << std::endl; // 输出: Found std::cout << (trie.search("appl") ? "Found" : "Not Found") << std::endl; // 输出: Not Found // 前缀匹配 std::cout << (trie.startsWith("app") ? "Prefix Exists" : "No Such Prefix") << std::endl; // 输出: Prefix Exists std::cout << (trie.startsWith("apz") ? "Prefix Exists" : "No Such Prefix") << std::endl; // 输出: No Such Prefix return 0; } ``` 此程序展示了如何创建一个简单的 Trie 并执行基本的操作,如插入、搜索和前缀匹配[^4]。 --- #### 关键概念解析 1. **节点设计** 节点类 (`TrieNode`) 是整个 Trie 结构的核心部分。每个节点存储指向其子节点的指针集合以及标记该节点是否为某单词结尾的信息[^3]。 2. **动态内存管理** 构造函数负责初始化根节点,析构函数递归释放所有分配的内存资源,防止泄漏。 3. **效率优化** 利用哈希表(`std::unordered_map`),可以在常数时间内访问任意字符对应的子节点,从而提高性能[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值