結果
| 問題 | No.3667 Prefix Count Queries |
| ユーザー |
|
| 提出日時 | 2026-08-31 21:11:05 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0) |
| 結果 |
AC
|
| 実行時間 | 140 ms / 2,000 ms |
| + 749µs | |
| コード長 | 2,165 bytes |
| 記録 | |
| コンパイル時間 | 2,058 ms |
| コンパイル使用メモリ | 341,596 KB |
| 実行使用メモリ | 38,148 KB |
| 最終ジャッジ日時 | 2026-09-01 00:51:46 |
| 合計ジャッジ時間 | 6,568 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// https://github.com/shingo0909/kyopro/blob/39029fab39c1735c38e5e9e4494d9934f81d6918/Library/Trie.cpp
struct Trie {
struct Node {
int per;
vector<int> next, accept;
int c, common;
Node(int c_, int per) : c(c_), common(0), per(per) {
next.assign(26, -1);
}
};
vector<Node> nodes;
int root;
Trie() : root(0) {
nodes.push_back(Node(root, -1));
}
void insert(string s) {
int id = 0;
for (char c : s) {
int num = c - 'a';
int &nid = nodes[id].next[num];
if (nid == -1) {
nid = nodes.size();
nodes.push_back(Node(num, id));
}
nodes[id].common++;
id = nid;
}
nodes[id].common++;
nodes[id].accept.push_back(nodes[0].common);
}
bool search(string s) {
int id = 0;
for (char c : s) {
int num = c - 'a';
int nid = nodes[id].next[num];
if (nid == -1) {
return false;
}
id = nid;
}
return nodes[id].accept.size() > 0;
}
int cur = 0;
void query1(char c) {
int num = c - 'a';
int &nid = nodes[cur].next[num];
if (nid == -1) {
nid = nodes.size();
nodes.push_back(Node(num, cur));
}
cur = nid;
}
void query2() {
cur = nodes[cur].per;
}
void query3() {
cout << nodes[cur].common << endl;
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
Trie t;
rep(i, n) {
string s;
cin >> s;
t.insert(s);
}
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
char c;
cin >> c;
t.query1(c);
}
if (op == 2) {
t.query2();
}
if (op == 3) {
t.query3();
}
}
return 0;
}