'commit'
This commit is contained in:
40
TangDou/AcWing/Trie/835_1.cpp
Normal file
40
TangDou/AcWing/Trie/835_1.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user