当前位置:网站首页>acwing 835. Trie string statistics
acwing 835. Trie string statistics
2022-06-22 01:50:00 【_ Liuxiaoyu】
Trie Trees : Efficiently store and find the data structure of string collection .
It's usually used trie , The title limits the type of characters 26 A or 52 individual .
If you want to trie Save the following set of strings in , The principle is as follows :
abcdef、abdef、aced、bcdf、bcff、cdaa、bcdc
Storage :
Generally speaking , Mark all the ending words , Otherwise, you can't find the word where it may be repeated .
lookup :
Go straight down one layer at a time , If there is trie A character that ends with a search string in the tree , be true, conversely ,false.
subject :
Maintain a collection of strings , Two operations are supported :
I x Insert a string into the collection x;
Q x Ask how many times a string appears in the collection .
share N Operations , The total length of the input string does not exceed 105, 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 x Number of occurrences in the set .
Each result takes up one line .
Data range
1≤N≤2∗104
sample input :
5
I abc
Q abc
Q ab
I ab
Q ab
sample output :
1
0
1
Ideas :
- trie Construction
- Each node is uniquely identified
- Each string has a flag at the end , Save the quantity
code:
#include <iostream>
using namespace std;
const int N = 100010;
// son[N][26] : N Express N layer
// Maximum per node 26, cnt Express By the end of N The number of strings at the end of nodes ,idx Index representation
int son[N][26], cnt[N], idx;
char str[N];
void insert(char str[])
{
int p = 0;
for(int i=0; str[i]; i++) // char str[] With '\0' ending , You can do this
{
int u = str[i] - 'a'; // Mapping to 0 -25
if(!son[p][u]) son[p][u] = ++idx; // If it doesn't exist , created , Each node has a unique number
p = son[p][u];
}
cnt[p] ++; // The number ending at this point + 1
}
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 m;
cin >> m;
while(m --)
{
char op[2];
cin >> op >> str;
if(op[0] == 'I') insert(str);
else cout << query(str) << endl;
}
return 0;
}
边栏推荐
- Intranet learning notes (3)
- 对标Copilot,国内首个:自然语言一键生成方法级代码aiXcoder XL来了
- Farm Game
- Standing at the digital tuyere, how can tooling enterprises "fly"
- 阿里腾讯百度软件测试工程师推荐——软件测试模型之快速原型模型
- MBA-day23 至多至少问题-练习题
- 【第 02 章 基于形态学的权重自适应图像去噪技术-全套系统MATLAB智能驾驶深度学习】
- 【第 07 章 基于主成分分析的人脸二维码识别MATLAB深度学习实战案例】
- BSV上的委托合约(2)
- SQL operation: with expression and its application
猜你喜欢
随机推荐
2021 CSP-J1 CSP-S1 第一轮 初赛 相关题解及视频等
acwing 836. 合并集合 (并查集)
2021 csp-j1 csp-s1 first round preliminary round related questions and videos
【第 14 章 基于主成分分析的图像压缩和重建--matlab深度学习实战案例】
2022年中国手机银行年度专题分析
Panic: permission denied problems encountered when using gomonkey mock functions and methods and Solutions
第 25 章 基于小波变换的数字水印技术
The 8th "Internet +" competition - Bi ran, an outstanding architect of Baidu, interprets the proposition of industrial circuit
High score schemes have been opened to the public, and the second round of the China "software Cup" remote sensing competition is coming!
【第 02 章 基于形态学的权重自适应图像去噪技术-全套系统MATLAB智能驾驶深度学习】
数字信号处理
Pyechart drawing word cloud
PHP admin deployment - resolve all errors
Redis implements distributed locks
twenty-one
2022年Q1手机银行用户规模达6.5亿,加强ESG个人金融产品创新
Navicat cannot connect to MySQL
Heidisql always makes errors when inserting data. What should I do
Divide the list into boxes and draw a histogram through pyechart
[tensorrt] video swing transformer deployment








