[模板]Trie

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 2e5 + 10;

int son[maxn][26], cnt[maxn], idx, n;
char op[2], s[maxn];

void Insert(char *str) {
  int p = 0;
  for (int i = 0; str[i]; i++) {
    int u = str[i] - 'a';
    if (!son[p][u]) son[p][u] = ++idx;
    p = son[p][u];
  }
  cnt[p]++;
}

int Query(char *str) {
  int p = 0;
  for (int i = 0; str[i]; i++) {
    int u = str[i] - 'a';
    if (!son[p][u]) return 0;
    p = son[p][u];
  }
  return cnt[p];
}


int main() {
  scanf("%d", &n);
  while (n--) {
    scanf("%s%s", op, s);
    if (op[0] == 'I') Insert(s);
    else printf("%d\n", Query(s));
  }
  return 0;
}

其实想说的是
数组可以开上40倍
但不要把数组开太大
不然会爆内存

posted @ 2021-10-26 23:05  _vv123  阅读(28)  评论(0编辑  收藏  举报