trie树(con[p][x]代表p沿着x方向走到的位置)
#include<iostream>
#include<string>
using namespace std;
const int N = 100010;
int con[N][26];
int cnt[N];
int n;
int idx = 0;
char opt;
string str;
void insert(string s) {
int p = 0;
for (int i = 0; i < s.size(); i ++ ) {
int x = s[i] - 'a';//确定坐标
if (!con[p][x]) con[p][x] = ++ idx; //p沿着x方向走到idx
p = con[p][x]; //更新p为idx,从idx再往下走
}
cnt[p] ++ ;
}
int query(string s) {
int p = 0;
for (int i = 0; i < s.size(); i ++ ) {
int x = s[i] - 'a';
if (!con[p][x]) return 0;
p = con[p][x];
}
return cnt[p];
}
int main() {
cin >> n;
while(n -- ) {
cin >> opt;
if (opt == 'I') {
cin >> str;
insert(str);
}
if (opt == 'Q') {
cin >> str;
cout << query(str) << endl;
}
}
}