![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2ba7cc137e664430a7238aba35fef1c6.png)
#include <iostream>
using namespace std;const int N = 1e6 + 10;
int son[N][26], cnt[N], idx;
void insert(string s)
{int p = 0; for (int i = 0; i < s.size(); i ++ ){int u = s[i] - 'a';if (!son[p][u]) son[p][u] = ++ idx;p = son[p][u];}cnt[p] ++ ;
}
int query(string s)
{int p = 0;for (int i = 0; i < s.size(); i ++ ){int u = s[i] - 'a';if (!son[p][u]) return 0;p = son[p][u];}return cnt[p];
}int main()
{int n;cin >> n;while (n -- ){char op;string x;cin >> op >> x;if (op == 'I'){insert(x);}else if (op == 'Q'){cout << query(x) << endl;}}return 0;
}