AcWing 835. Trie字符串统计
原题链接
简单
作者:
pdsuacm02
,
2021-05-27 19:45:46
,
所有人可见
,
阅读 221
C++ 代码
#include <iostream>
using namespace std;
const int N = 100010;
int son[N][26],cnt[N],idx;
void insert(char s[])
{
int p = 0;
for(int i = 0;s[i];i++){
int u = s[i] - 'a';
if(!son[p][u]) son[p][u] = ++idx;
p = son[p][u];
}
cnt[p]++;
}
int querry(char s[])
{
int p = 0;
for(int i = 0;s[i];i++){
int u = s[i] - 'a';
if(!son[p][u]) return 0;
p = son[p][u];
}
return cnt[p];
}
int main()
{
int t;
cin >> t;
while (t--)
{
char a;
cin >> a;
char s[N];
cin >> s;
if(a == 'I') insert(s);
else cout << querry(s)<<endl;
}
}