记住一件事情即可:Trie是高效存储和查找字符串集合的数据结构
一般来说题目是会限制字母的种类,不会太多
#include <iostream>#include <cstring>#include <string>#include <cmath>#include <cstdio>#include <stdio.h>#include <cstdlib>#include <algorithm>#include <vector>#include <set>#include <map>#include <iomanip>#define rep(i,a,b) for(int i = a; i <= b ; i ++ )#define pre(i,a,b) for(int i = b ; i >= a ; i --)#define ll long long#define inf 0x3f3f3f3f#define ull unsigned long long#define ios ios::sync_with_stdio(false),cin.tie(0)using namespace std;typedef pair<int,int> PII ;const int N = 2e6 + 10;int son[N][26],cnt[N],idx; //下标是0的点,既是根节点,也是空节点char str[N];int n;void insert_string(char *str){ int root=0; for(int i=0;str[i];i++) { int u=str[i]-‘a‘; if(!son[root][u]) son[root][u]=++idx; root=son[root][u]; } cnt[root]++;}int query(char *str){ int root=0; for(int i=0;str[i];i++) { int u=str[i]-‘a‘; if(!son[root][u]){ return 0; } root=son[root][u]; } return cnt[root];}int main(){ ios; cin>>n; while(n--) { char op[2]; cin>>op>>str; if(op[0]==‘I‘) { insert_string(str); } else{ cout<<query(str)<<endl; } } return 0;}