結果

問題 No.3667 Prefix Count Queries
ユーザー Unbakedbread
提出日時 2026-09-13 22:33:00
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 181 ms / 2,000 ms
+ 98µs
コード長 856 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,090 ms
コンパイル使用メモリ 337,100 KB
実行使用メモリ 32,744 KB
最終ジャッジ日時 2026-09-13 22:33:12
合計ジャッジ時間 8,679 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

struct Node {
	int p, val;
	array<int, 26> v;
	Node(int p) : p(p), val(0) {for(int i = 0; i < 26; ++i) v[i] = -1;}
};

vector<Node> v = {Node(-1)};

int move(int t, char c) {
	const int a = c - 'a';
	if(v[t].v[a] == -1) {
		v[t].v[a] = v.size();
		v.push_back(Node(t));
	}
	return v[t].v[a];
}

void insert(string S) {
	int now = 0;
	v[now].val += 1;
	for(auto c : S) {
		now = move(now, c);
		v[now].val += 1;
	}
}

int main() {
	int N;
	cin >> N;
	vector<string> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];
	
	for(int i = 0; i < N; ++i) insert(A[i]);
	
	int Q;
	cin >> Q;
	
	int now = 0;
	while(Q--) {
		int cmd;
		cin >> cmd;
		
		if(cmd == 1) {
			char x;
			cin >> x;
			
			now = move(now, x);
		}
		
		if(cmd == 2) {
			now = v[now].p;
		}
		
		if(cmd == 3) {
			cout << v[now].val << "\n";
		}
	}
}
0