当前位置:网站首页>(板子)Trie树模板 AcWing835. Trie字符串统计
(板子)Trie树模板 AcWing835. Trie字符串统计
2022-07-15 20:27:00 【咸蛋_dd】
维护一个字符串集合,支持两种操作:
I x向集合中插入一个字符串 xx;Q x询问一个字符串在集合中出现了多少次。
共有 NN 个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母。
输入格式
第一行包含整数 NN,表示操作数。
接下来 NN 行,每行包含一个操作指令,指令为 I x 或 Q x 中的一种。
输出格式
对于每个询问指令 Q x,都要输出一个整数作为结果,表示 xx 在集合中出现的次数。
每个结果占一行。
数据范围
1≤N≤2∗1041≤N≤2∗104
输入样例:
5
I abc
Q abc
Q ab
I ab
Q ab
输出样例:
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
int idx=0,cnt[N],son[N][30];
void Insert(char *str)
{
int p=0;
for(int i=0;str[i];i++)
{
int u=str[i]-'a';
if(son[p][u]==0)
son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
}
int ques(char *str)
{
int p=0;
for(int i=0;str[i];i++)
{
int u=str[i]-'a';
if(son[p][u]==0)
return 0;
p=son[p][u];
}
return cnt[p];
}
int main()
{
int n,k;
char str[N];
scanf("%d",&n);
while(n--)
{
getchar();
char c;
scanf("%c",&c);
if(c=='I')
{
getchar();
cin>>str;
Insert(str);
}
else
{
getchar();
cin>>str;
k=ques(str);
printf("%d\n",k);
}
}
return 0;
}
1
0
1边栏推荐
- Centos7.2 install mysql5.7 through RPM package
- Nodejs直接使用proto文件
- C语言爱心代码大全(2022表白神器)
- Codeforces Round #802 C. Helping the Nature
- 递归函数,求阶乘
- [Space & single cell omics] phase 2: combined single cell and bulk transcriptome to identify two epithelial tumor cell states in colorectal cancer, and improved CMS typing
- 8个关于 Promise.then 和 Promise.catch 的面试题,一定要掌握
- 接口测试实战 - 学生信息管理系统
- Is it safe for Huatai Securities to open an account online? Is the fund guaranteed?
- 功能、模块质量和非功能性测试
猜你喜欢

datax扩展vertica插件

in_array的第3个参数实例分析

Answer the reader's question (6): how to analyze the single cell TPM matrix?

Nest 框架

Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening

Codeforces Global Round 21 D. Permutation Graph

透视北交所100家上市公司:中年理工男的新世界

Unity shader - cginclude file cginc

LeetCode:735. 行星碰撞————中等

LeetCode-162-寻找峰值(二分必看)
随机推荐
面试难题:怎么不用定时任务实现关闭订单?
同花顺股票开户安全吗 reits基金怎么购买
svg loading动画
Perspective of 100 listed companies on the Beijing stock exchange: the new world of middle-aged men of science and technology
Laravel 异步执行任务
Shiro integrates redis to realize distributed session processing
AWS Config
Microservice architecture | service isolation - [gateway]
Seaborn - draw multi label confusion matrix, recall, accuracy, F1
MySQL的意向共享锁、意向排它锁和死锁是什么
LeetCode-240-搜索二维矩阵Ⅱ
Leetcode-162- find the peak value (two points must be seen)
Codeforces Round #803 (Div. 2) B. Rising Sand
【物联网】WiFi基础知识 (二)【看评论区领取资料】
苏宁易购的api接口展示
Codeforces Round #802 D. River Locks
Codeforces Global Round 21 D. Permutation Graph
Exness: crude oil stopped falling and rebounded. Pay attention to the performance of US terrorist data in the evening
Leetcode-62-different paths
CAN通信(1)——CAN通信物理层