当前位置:网站首页>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;
}
边栏推荐
- Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
- Microcontroller: temperature control DS18B20
- FPGA: Use of the development environment Vivado
- 单片机:温度控制DS18B20
- Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
- SD NAND Flash简介!
- MySQL事务
- This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- 第五章:多线程通信—wait和notify
猜你喜欢
随机推荐
我们的Web3创业项目,黄了
[Android] How to use RecycleView in Kotlin project
数据中台建设(十):数据安全管理
Go编译原理系列6(类型检查)
第三章 : redis数据结构种类
Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
你最隐秘的性格在哪?
three objects are arranged in a spherical shape around the circumference
Chapter 5: Multithreaded Communication—wait and notify
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
【Unity】【UGUI】【在屏幕上显示文本】
[Unity] [UGUI] [Display text on the screen]
Voice-based social software development - making the most of its value
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
GCC编译的时候头文件搜索规则
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
电竞、便捷、高效、安全,盘点OriginOS功能的关键词








