当前位置:网站首页>Trie string statistics
Trie string statistics
2022-06-28 06:26:00 【MITBlick】
Maintain a collection of strings , Two operations are supported :
I xInsert a string into the collection x;Q xAsk how many times a string appears in the collection .
share NN Operations , The total length of the input string does not exceed 1e5, The string contains only lowercase letters .
Input format
The first line contains integers N, Represent operands .
Next N That's ok , Each line contains an operation instruction , Instructions for I x or Q x One of the .
Output format
For each inquiry instruction Q x, You have to output an integer as the result , Express xx Number of occurrences in the set .
Each result takes up one line .
Data range
1≤N≤2∗1e4
sample input :
5
I abc
Q abc
Q ab
I ab
Q ab
sample output :
1
0
1Code:
#include <iostream>
#include <stdio.h>
using namespace std;
typedef int Status;
const int N = 100010;
int n, idx, cnt[N], son[N][26];
char str[N], op[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] ++; // Count
}
Status query(char str[])
{
int p = 0;
for(int i = 0; str[i]; i ++ )
{
int u = str[i] - 'a';
if(!son[p][u]) return 0; // Stop without this string
p = son[p][u];
}
return cnt[p];
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
{
cin >> op >> str;
if(op[0] == 'I') insert(str);
else printf("%d\n", query(str));
}
}边栏推荐
- Openharmony gnawing paper growth plan -- json-rpc
- 阿里云短信服务(完整指南),短信发送功能实现。
- Development trend of mobile advertising: Leveraging stock and fine marketing
- Linux Mysql 实现root用户不用密码登录
- No one can only use foreach to traverse arrays, right?
- Exception handling (I) -- null pointer and array index out of bounds
- Caused by: com. fasterxml. jackson. databind. exc.InvalidDefinitionException: Cannot construct instance
- sql及list去重操作
- Drop down list processing in Web Automation
- YYGH-BUG-02
猜你喜欢
随机推荐
Caused by: com. fasterxml. jackson. databind. exc.InvalidDefinitionException: Cannot construct instance
使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量
AutoCAD C polyline self intersection detection
Tryout title code
socke.io长连接实现推送、版本控制、实时活跃用户量统计
Development trend of mobile advertising: Leveraging stock and fine marketing
【网络教程】IPtables官方教程--学习笔记1
Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
Iframe switching in Web Automation
JSP
AutoCAD C# 多段线小锐角检测
ThreadLocal
Differences between overloads, rewrites, abstract classes and interfaces
ImportError: cannot import name 'ensure_dir_exists'的可解决办法
Unity packaging webgl uses IIS to solve the error
链表(二)——设计链表
Common basic functions of Oracle
Oracle condition, circular statement
Camx架构开UMD、KMD log以及dump图的方式
4~20mA输入/0~5V输出的I/V转换电路









