You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
775 B
40 lines
775 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 1e5 + 10;
|
|
int tr[N][26], idx, cnt[N];
|
|
|
|
void insert(string str) {
|
|
int p = 0;
|
|
for (int i = 0; i < str.size(); i++) {
|
|
int u = str[i] - 'a';
|
|
if (!tr[p][u]) tr[p][u] = ++idx;
|
|
p = tr[p][u];
|
|
}
|
|
cnt[p]++;
|
|
}
|
|
|
|
int query(string str) {
|
|
int p = 0;
|
|
for (int i = 0; i < str.size(); i++) {
|
|
int u = str[i] - 'a';
|
|
if (!tr[p][u]) return 0;
|
|
p = tr[p][u];
|
|
}
|
|
return cnt[p];
|
|
}
|
|
|
|
int main() {
|
|
int n;
|
|
cin >> n;
|
|
while (n--) {
|
|
char op;
|
|
string str;
|
|
cin >> op >> str;
|
|
if (op == 'I')
|
|
insert(str);
|
|
else
|
|
printf("%d\n", query(str));
|
|
}
|
|
return 0;
|
|
} |