当前位置:网站首页>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;
}
边栏推荐
- Go编译原理系列6(类型检查)
- 【温度预警程序de开发】事件驱动模型实例运用
- FPGA:开发环境Vivado的使用
- 告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
- 首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
- Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
- Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
- 什么是 DevOps?看这一篇就够了!
- 登录功能和退出功能(瑞吉外卖)
- Microcontroller: temperature control DS18B20
猜你喜欢
three.js调试工具dat.gui使用
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
SQL Outer Join Intersection, Union, Difference Query
Data Middle Office Construction (10): Data Security Management
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
什么是 DevOps?看这一篇就够了!
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
随机推荐
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
Go编译原理系列6(类型检查)
NowCoderTOP35-40 - continuous update ing
Offensive World-PWN-new_easypwn
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
第八章:activiti多用户任务分配
Leetcode刷题——623. 在二叉树中增加一行
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
[Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
Brief Analysis of WSGI Protocol
【 temperature warning program DE development 】 event driven model instance
你最隐秘的性格在哪?
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
登录功能和退出功能(瑞吉外卖)
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
我们的Web3创业项目,黄了
How can project cost control help project success?
three物体围绕一周呈球形排列