当前位置:网站首页>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;
}
边栏推荐
- 技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
- 产品太多了,如何实现一次登录多产品互通?
- Header file search rules when compiling with GCC
- 一文道清什么是SPL
- GCC编译的时候头文件搜索规则
- 【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
- linux下oracle常见操作以及日常积累知识点(函数、定时任务)
- uniapp connect ibeacon
- Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
- How can project cost control help project success?
猜你喜欢

Voice-based social software development - making the most of its value

The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller

这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口

什么是 DevOps?看这一篇就够了!

Opencv图像缩放和平移

Opencv算术操作

E-sports, convenience, efficiency, security, key words for OriginOS functions

PCB布局必知必会:教你正确地布设运算放大器的电路板

Our Web3 Entrepreneurship Project, Yellow

数据可视化(一)
随机推荐
Introduction to SD NAND Flash!
秘乐短视频挖矿系统开发详情
Create a Dapp, why choose Polkadot?
nyoj754 黑心医生 结构体优先队列
Complete image segmentation efficiently based on MindSpore and realize Dice!
three.js调试工具dat.gui使用
FPGA: Use of the development environment Vivado
Go compilation principle series 6 (type checking)
60行从零开始自己动手写FutureTask是什么体验?
如何选币与确定对应策略研究
How does the official account operate and maintain?Public account operation and maintenance professional team
百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
How to choose coins and determine the corresponding strategy research
[Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
【OpenCV】-仿射变换
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
Data Middle Office Construction (10): Data Security Management
你最隐秘的性格在哪?
2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]