当前位置:网站首页>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;
}
边栏推荐
- 第六章:activiti流程分流判断之排它网关和并行网关
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
- three objects are arranged in a spherical shape around the circumference
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- 七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
- Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
- 华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
- LeetCode 216. Combined Sum III (2022.08.04)
- Go编译原理系列6(类型检查)
猜你喜欢

Opencv算术操作

three.js调试工具dat.gui使用

NowCoderTOP35-40 - continuous update ing

Dynamics 365Online PDF导出及打印

2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing

The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?

Jenkins manual (2) - software configuration

登录功能和退出功能(瑞吉外卖)

还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!

创建一个 Dapp,为什么要选择波卡?
随机推荐
uniapp connect ibeacon
FPGA:开发环境Vivado的使用
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
第四章:redis 数组结构的set和一些通用命令「建议收藏」
19.3 restart the Oracle environment
PCB布局必知必会:教你正确地布设运算放大器的电路板
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
GCC编译的时候头文件搜索规则
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
如何测试一下现场的备机失败,转发主机的场景?
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
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
PHP operation mangoDb
Login function and logout function (St. Regis Takeaway)
uniapp 连接ibeacon
GPU-CUDA-图形渲染分析
第六章:activiti流程分流判断之排它网关和并行网关
登录功能和退出功能(瑞吉外卖)