当前位置:网站首页>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));
}
}边栏推荐
- Differences between basic types and packaging classes
- Difficulty calculation of Ethereum classic
- socke.io长连接实现推送、版本控制、实时活跃用户量统计
- Integer promotion and size side byte order
- Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance
- AutoCAD C polyline small acute angle detection
- Install redis on windows and permanently change the password, and integrate redis with the SSM framework
- 基本类型和包装类的区别
- Some habits of it veterans in the workplace
- [staff] arpeggio mark
猜你喜欢

Linked list (I) - remove linked list elements

Oracle condition, circular statement

ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map

Boost the rising point | yolov5 combined with alpha IOU

freeswitch使用mod_shout模块播放mp3

death_ satan/hyperf-validate

AutoCAD C# 多段线自相交检测

Unity packaging webgl uses IIS to solve the error

Batch import of pictures into WPS table by date

The custom cube UI pop-up dialog supports multiple and multiple types of input boxes
随机推荐
选拔赛题目代码
报错--解决core-js/modules/es.error.cause.js报错
Install redis on windows and permanently change the password, and integrate redis with the SSM framework
图片按日期批量导入WPS表格
Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
MySQL (I) - Installation
mysql常用函数
AutoCAD C polyline small acute angle detection
异常处理(一)——空指针和数组索引越界
API learning of OpenGL (2006) glclientactivetexture
Idea generates entity classes from database tables
ThreadLocal
慢内容广告:品牌增长的长线主义
Idea automatically adds comments when creating classes
AutoCAD C polyline self intersection detection
借助nz-pagination中的let-total解析ng-template
Batch import of pictures into WPS table by date
整型提昇和大小端字節序
FPGA - 7 Series FPGA selectio -07- iserdese2 of advanced logic resources
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance