AcWing 835. 如何理解单(双)链表,Trie树和堆中的idx? - AcWing
#include<bits/stdc++.h>
using namespace std;
int n;
int Trie[100010][26], cnt[100010], idx;
void _insert(string str)
{
int p = 0;
int len = str.length();
for(int i=0;i<len;++i)
{
int u = str[i] - 'a';
if(!Trie[p][u]) Trie[p][u] = ++idx;
p = Trie[p][u];
}
cnt[p]++;
}
int _query(string str)
{
int p = 0;
int len = str.length();
for(int i=0;i<len;++i)
{
int u = str[i] - 'a';
if(!Trie[p][u]) return 0;
p = Trie[p][u];
}
return cnt[p];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin>>n;
char ch;
string s;
for(int i=0;i<n;++i)
{
cin>>ch>>s;
if(ch == 'I')
_insert(s);
else
cout<<_query(s)<<'\n';
}
return 0;
}