当前位置:网站首页>trie树模板
trie树模板
2022-08-05 10:28:00 【一条小小yu】
题目:Trie字符串统计
维护一个字符串集合,支持两种操作:
I x向集合中插入一个字符串 xx;Q x询问一个字符串在集合中出现了多少次。
共有 N个操作,输入的字符串总长度不超过 10^5,字符串仅包含小写英文字母。
输入格式
第一行包含整数 N,表示操作数。
接下来 N 行,每行包含一个操作指令,指令为 I x 或 Q x 中的一种。
输出格式
对于每个询问指令 Q x,都要输出一个整数作为结果,表示 x 在集合中出现的次数。
每个结果占一行。
数据范围
1≤N≤2∗10^4 1≤N≤2∗10^4
输入样例:
5
I abc
Q abc
Q ab
I ab
Q ab
输出样例:
1
0
1#include<iostream>
using namespace std;
const int N=1e5+10;
int son[N][26],cnt[N],idx;
char str[N];
void insert(char str[])
{
int p=0;
for(int i=0;str[i];i++)
{
int u=str[i]-'a';
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
}
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 n;
scanf("%d",&n);
while(n--)
{
char op[2];
scanf("%s%s",op,str);
if(op[0]=='I')insert(str);
else printf("%d\n",query(str));
}
return 0;
}
边栏推荐
- 数据可视化(二)
- three物体围绕一周呈球形排列
- 单片机:温度控制DS18B20
- 技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
- Brief Analysis of WSGI Protocol
- 阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
- How to choose coins and determine the corresponding strategy research
- Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
- Opencv图像缩放和平移
- Oracle 19.3 restart 环境
猜你喜欢
随机推荐
深入理解 Istio 流量管理的超时时间设置
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
Score interview (1)----related to business
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
力扣(LeetCode)216. 组合总和 III(2022.08.04)
[Strong Net Cup 2022] WP-UM
Opencv算术操作
公众号如何运维?公众号运维专业团队
[Unity] [UGUI] [Display text on the screen]
【 temperature warning program DE development 】 event driven model instance
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
[Android] How to use RecycleView in Kotlin project
MySQL transactions
你最隐秘的性格在哪?
如何测试一下现场的备机失败,转发主机的场景?
three objects are arranged in a spherical shape around the circumference
【温度预警程序de开发】事件驱动模型实例运用
FPGA:开发环境Vivado的使用
R语言ggplot2可视化:可视化密度图(Density plot)、可视化多个分组的密度图、数据点分布在箱图中间、添加主标题、副标题、题注信息
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing







![[强网杯2022]WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
