字典树(Trie 树)
444 字
2 分钟
字典树(Trie 树)
1. 题目描述
给定 个模式串 和 次询问,每次询问给定一个文本串 ,请回答 中有多少个字符串 满足 是 的前缀。
一个字符串 是 的前缀当且仅当从 的末尾删去若干个(可以为 0 个)连续的字符后与 相同。
输入的字符串大小敏感。例如,字符串 Fusu 和字符串 fusu 不同。
2. 解析
对于每一个模式字符串,从第一个字符到最后一个字符建立一棵树,并给路径上的每一个标记点加上 。输入检查字符串的时候,顺着树上的匹配路径往下走,一直走到不能再走,如果前面没有匹配的字符串了,说明答案就是 。如果结束为止在树的某个节点上,答案就是这个点的标记数目。
3. 代码
#include <bits/stdc++.h>using namespace std;int q,n,m;struct Node{ vector <Node> children; char val; int cnt;};int main(){ cin>>q; while(q--){ cin>>n>>m; Node formatStr; for(int i=1;i<=n;i++){ string s; cin>>s; // ptr to root Node *ptr=&formatStr; for(auto c:s){ bool flag=1; for(auto &child:ptr->children){ if(child.val==c){ child.cnt++; ptr=&child; flag=0; break; } } if(flag){ // 说明没有现成的节点 Node newNode; newNode.val=c; newNode.cnt=1; ptr->children.push_back(newNode); ptr=&ptr->children.back(); } } } for(int i=1;i<=m;i++){ string s; cin>>s; Node *ptr=&formatStr; bool flag=1; int findCnt=0; for(auto c:s){ for(auto &child:ptr->children){ if(child.val==c){ findCnt++; ptr=&child; flag=0; break; } } if(flag){ // 说明路走不通了。 break; } } if(findCnt==s.size()){ cout<<ptr->cnt<<endl; }else{ cout<<0<<endl; } } } return 0;}然而,这样的代码的运行速度不太理想,我们可以进一步优化。

考虑在查找子节点时降到 的复杂度,用 存子节点,就能便捷地进行二分查找。
struct Node{ set <Node> children; char val; int cnt; // 重定义小于号 bool operator < (const Node &rhs) const{ return val<rhs.val; }};支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!
相关文章 智能推荐
1
CSP 考前膜拜模版
笔记本 2023-10-20
2
CSP初赛单选整理
笔记本 2023-09-10
3
成都的照片
展览厅 分享一些在成都拍的照片
4
在受限 Docker 容器(2核4GB)内通过 QEMU 运行完整 Ubuntu 22.04
实验室 详细记录在无法使用 KVM 的受限 Docker 环境中,使用 QEMU 软件模拟 + Cloud-Init 成功运行 Ubuntu 虚拟机的完整流程、踩坑记录与性能优化。
5
三星平板 SM-P615C 刷机 LineageOS
实验室 学海平板洋垃圾太卡了,刷新系统焕发新生。
随机文章 随机推荐
无穷量?