当前位置:网站首页>acwing 835. Trie字符串统计
acwing 835. Trie字符串统计
2022-06-22 01:16:00 【_刘小雨】
Trie 树 : 高效地存储和查找字符串集合的数据结构。
一般用trie ,题目限制了字符种类26个或者52个。
如果想在trie 中保存下面一组字符串,原理如下:
abcdef、abdef、aced、bcdf、bcff、cdaa、bcdc
存储:
一般来说,会在所有结尾的单词标记一下,不然在有可能重复的地方找不到该单词。
查找:
直接进行一层一层下来找,如果有在trie树中以查找字符串结束的字符,则true,反之,false。
题目:
维护一个字符串集合,支持两种操作:
I x 向集合中插入一个字符串 x;
Q x 询问一个字符串在集合中出现了多少次。
共有 N 个操作,输入的字符串总长度不超过 105,字符串仅包含小写英文字母。
输入格式
第一行包含整数 N,表示操作数。
接下来 N 行,每行包含一个操作指令,指令为 I x 或 Q x 中的一种。
输出格式
对于每个询问指令 Q x,都要输出一个整数作为结果,表示 x 在集合中出现的次数。
每个结果占一行。
数据范围
1≤N≤2∗104
输入样例:
5
I abc
Q abc
Q ab
I ab
Q ab
输出样例:
1
0
1
思路:
- trie 的构造
- 每个节点唯一标识
- 每个字符串结尾都有标志,保存数量
code:
#include <iostream>
using namespace std;
const int N = 100010;
// son[N][26] : N 表示有N层
// 每个节点的最多26, cnt表示 以第N个节点结尾字符串的个数,idx表示索引
int son[N][26], cnt[N], idx;
char str[N];
void insert(char str[])
{
int p = 0;
for(int i=0; str[i]; i++) // char str[]以'\0' 结尾,可以这样弄
{
int u = str[i] - 'a'; // 映射到0 -25
if(!son[p][u]) son[p][u] = ++idx; // 如果不存在,创建出来,每个节点唯一编号
p = son[p][u];
}
cnt[p] ++; // 以这个点结尾的数量 + 1
}
int query(char str[])
{
int p = 0;
for(int i=0; str[i]; i++)
{
int u = str[i] - 'a';
if(!son[p][u]) return 0;
p = son[p][u];
}
return cnt[p];
}
int main()
{
int m;
cin >> m;
while(m --)
{
char op[2];
cin >> op >> str;
if(op[0] == 'I') insert(str);
else cout << query(str) << endl;
}
return 0;
}
边栏推荐
- 【第 17 章 基于 Harris 的角点特征检测--Matlab机器学习项目实战】
- 使用 gomonkey Mock 函数及方法
- Creating a successful paradigm for cross-border e-commerce: Amazon cloud technology helps sellers lay out the next growth point
- 【第 20 章 基于帧间差法进行视频目标检测--MATLAB软件深度学习应用】
- 一条短视频成本几十万元,虚拟数字人凭“实力”出圈
- linxu 将文件夹的权限修改为所有人可以访问 777
- Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
- 亚马逊测评浏览器,亚马逊测评风控核心知识点
- SAP MM 进口采购业务中供应商多送或者少送场景的处理
- Apache Doris real-time data analysis nanny level tutorial
猜你喜欢

同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会

ROS 2 驱动程序现在可用于 ABB 的机械臂

Function test - Introduction to MySQL database

技术探秘: 360数科夺得ICDAR OCR竞赛世界第一

MBA-day24 最值问题

Machine learning pytoch implementation case LSTM case (flight number prediction)

【数论】leetcode1010. Pairs of Songs With Total Durations Divisible by 60

Brief introduction to jpom: simple and light low intrusive online construction, automatic deployment, daily operation and maintenance, and project monitoring software

PHP admin deployment - resolve all errors

【第 26 章 基于最小误差法和区域生长的医学影响分割系统--matlab深度学习实战GUI项目】
随机推荐
Yang Bing: oceanbase helps digital transformation, and native distributed database becomes the first choice for core system
MySql DUMP 自动备份数据库 Shell 脚本
21
求一个防关联检测工具,浏览器指纹在线检测
Navicat连接不到MySQL
第 25 章 基于小波变换的数字水印技术
Application of C language dynamic memory function
C语言动态内存函数的应用
功能测试——MySQL数据库简介
[solution] Ming Chu Liang Zao video edge computing gateway solution
Intranet learning notes (3)
2022年中国手机银行年度专题分析
带你区分几种并行
Apache Doris实时数据分析保姆级使用教程
Use gomonkey mock function and method
ASEMI肖特基二极管1N5819参数,1N5819代换,1N5819货源
DAST 黑盒漏洞扫描器 第四篇:扫描性能
Fabric.js IText 手动设置斜体
Machine learning pytoch implementation case LSTM case (flight number prediction)
第 24 章 基于 Simulink 进行图像和视频处理--matlab深度学习实战整理