当前位置:网站首页>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));
}
}边栏推荐
- Linked list (III) - reverse linked list
- MySQL (I) - Installation
- 4~20mA输入/0~5V输出的I/V转换电路
- 基本类型和包装类的区别
- Use of JDBC
- Differences between overloads, rewrites, abstract classes and interfaces
- Create a gson object that formats the time zone. JSON parsing time formatting zoneddatetime
- Paper recommendation: efficientnetv2 - get smaller models and faster training speed through NAS, scaling and fused mbconv
- CAD二次开发+NetTopologySuite+PGIS 引用多版本DLL问题
- 【网络教程】IPtables官方教程--学习笔记1
猜你喜欢

Openharmony gnawing paper growth plan -- json-rpc

Unity packaging webgl uses IIS to solve the error

Freeswitch使用originate转dialplan

Linked list (III) - reverse linked list

Exception handling (I) -- null pointer and array index out of bounds

FPGA - 7 Series FPGA selectio -09- io of advanced logic resources_ FIFO

Development trend of mobile advertising: Leveraging stock and fine marketing

Install and manage multiple versions of PHP under mac

Small ball playing

FPGA - 7系列 FPGA SelectIO -09- 高级逻辑资源之IO_FIFO
随机推荐
Alert pop-up processing in Web Automation
Yygh-6-wechat login
AttributeError: 'callable_ iterator' object has no attribute 'next'
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
Object object to list collection
SQL and list de duplication
链表(一)——移除链表元素
Floating and positioning
The custom cube UI pop-up dialog supports multiple and multiple types of input boxes
Linux MySQL implements root user login without password
Yolact++ pytoch environment
Shell script one click deployment (MySQL)
Batch import of pictures into WPS table by date
Students who do not understand the code can also send their own token. The current universal dividend model can be divided into BSC and any generation B
Teach you how to use UCOS
Freeswitch uses Mod_ Shot module plays mp3
整型提昇和大小端字節序
Promotion intégrale et ordre des octets de fin de taille
Difficulty calculation of Ethereum classic
【星海出品】 运维巡检合集