結果

問題 No.3667 Prefix Count Queries
ユーザー tnakao0123
提出日時 2026-09-01 10:01:09
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 68 ms / 2,000 ms
+ 838µs
コード長 1,401 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 159 ms
コンパイル使用メモリ 53,092 KB
実行使用メモリ 51,072 KB
最終ジャッジ日時 2026-09-01 10:01:16
合計ジャッジ時間 4,654 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3667.cc:  No.3667 Prefix Count Queries - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_QN = 200000;

/* typedef */

struct Node {
  Node *cs[26], *p;
  int f, ci;
  Node(Node *_p = nullptr): cs(), p(_p), f(), ci() {}
};

/* global variables */

char s[MAX_N + 4];
Node *root;

/* subroutines */

void trie_add(char s[]) {
  Node *u = root;
  for (int i = 0; s[i]; i++) {
    int si = s[i] - 'a';
    if (u->cs[si] == nullptr) u->cs[si] = new Node(u);
    u = u->cs[si];
  }
  u->f++;
}

void trie_setall() {
  for (Node *u = root; u != nullptr;) {
    if (u->ci < 26) {
      auto v = u->cs[u->ci++];
      if (v != nullptr) {
	u = v;
      }
    }
    else {
      if (u->p != nullptr) u->p->f += u->f;
      u = u->p;
    }
  }
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  root = new Node();
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    trie_add(s);
  }

  trie_setall();
  
  int qn;
  scanf("%d", &qn);

  Node *u = root;
  while (qn--) {
    int op;
    scanf("%d", &op);

    if (op == 1) {
      char xs[4];
      scanf("%s", xs);

      int x = xs[0] - 'a';
      if (u->cs[x] == nullptr) u->cs[x] = new Node(u);
      u = u->cs[x];
    }
    else if (op == 2) {
      u = u->p;
    }
    else {
      printf("%d\n", u->f);
    }
  }
  
  return 0;
}

0