当前位置:网站首页>String - Trie
String - Trie
2022-08-01 22:07:00 【ThXe】
Trie
维护一个字符串集合,支持两种操作:
I x 向集合中插入一个字符串 x;
Q x 询问一个字符串在集合中出现了多少次.
Suppose the sum of all string lengths is n,The time complexity of building a dictionary tree is O(n)
Suppose the length of the string you are looking for is k,查找的时间复杂度为O(k)
拓展应用:
1.A string is a prefix of how many strings are in the collection
解法:Each node can be counted
//Trie树快速存储字符集合和快速查询字符集合
//支持大小写+数字
#include<bits/stdc++.h>
using namespace std;
const int N = 3000001;
//son[][]存储子节点的位置,分支最多26条;
//cnt[]存储以某节点结尾的字符串个数(同时也起标记作用)
//idx表示当前要插入的节点是第几个,每创建一个节点值+1
int son[N][65], cnt[N], idx;
char str[N];
int getnum(char x) {
if (x >= 'A' && x <= 'Z')
return x - 'A';
else if (x >= 'a' && x <= 'z')
return x - 'a' + 26;
else
return x - '0' + 52;
}
void insert(char* str)
{
int p = 0; //类似指针,指向当前节点
for (int i = 0; str[i]; i++)
{
int u =getnum(str[i]); //将字母转化为数字
if (!son[p][u]) son[p][u] = ++idx; //该节点不存在,创建节点
p = son[p][u]; //使“p指针”指向下一个节点
cnt[p]++; //结束时的标记,也是记录以此节点结束的字符串个数(前缀)
}// cnt[p]++出现次数
}
int query(char* str)
{
int p = 0;
for (int i = 0; str[i]; i++)
{
int u = getnum(str[i]);
if (!son[p][u]) return 0; //该节点不存在,即该字符串不存在
p = son[p][u];
}
return cnt[p]; //返回字符串出现的次数
}
int main()
{
int t; cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
for (int i = 0; i <= idx; i++)
for (int j = 0; j <= 64; j++)
son[i][j] = 0;
for (int i = 0; i <= idx; i++)
cnt[i] = 0;
idx = 0;
for (int i = 1; i <= n; i++)
{
cin >> str;
insert(str);
}
for (int i = 1; i <= m; i++)
{
cin >> str;
cout << query(str)<<endl;
}
}
return 0;
}
边栏推荐
- 03、GO语言变量定义、函数
- render-props and higher order components
- 力扣第 304 场周赛复盘
- 高等代数_证明_矩阵的任意特征值的代数重数大于等于其几何重数
- 线上故障排查方案
- SQL injection of WEB penetration
- FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
- 毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
- 你居然不懂Bitmap和Drawable? 相关知识大扫盲
- 教你VSCode如何快速对齐代码、格式化代码
猜你喜欢

render-props and higher order components

小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT

网络水军第一课:手写自动弹幕

blender3.2.1 unit setting

程序员必备的 “ 摸鱼神器 ” 来了 !

Based on php Xiangxi tourism website management system acquisition (php graduation design)

小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书

2022-08-01 第八组 曹雨 泛型 枚举

Shell programming conditional statement

Still struggling with reporting tool selection?To take a look at this
随机推荐
第一讲 测试知多少
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
Based on php Xiangxi tourism website management system acquisition (php graduation design)
微软校园大使喊你来秋招啦!
SOM Network 2: Implementation of the Code
Unity Shader general lighting model code finishing
Analysis of the development trend of game metaverse
你居然不懂Bitmap和Drawable? 相关知识大扫盲
工程建筑行业数据中台指标分析
scikit-learn no moudule named six
KMP 字符串匹配问题
ModuleNotFoundError: No module named ‘yaml‘
shell规范与变量
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
long investment career
小程序毕设作品之微信美食菜谱小程序毕业设计成品(8)毕业设计论文模板
2022 版 MySQL 巅峰教程,收藏好,慢慢看
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
还在纠结报表工具的选型么?来看看这个
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告