結果
問題 |
No.2761 Substitute and Search
|
ユーザー |
![]() |
提出日時 | 2024-05-17 22:45:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 609 ms / 4,000 ms |
コード長 | 1,693 bytes |
コンパイル時間 | 1,996 ms |
コンパイル使用メモリ | 206,676 KB |
最終ジャッジ日時 | 2025-02-21 15:12:08 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 13 |
ソースコード
#include <bits/stdc++.h> using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } struct TrieNode { int count; map<char, TrieNode*> children; TrieNode() : count(0) {} }; void insert(TrieNode* root, const string& word) { TrieNode* current = root; for (char c : word) { if (current->children.find(c) == current->children.end()) { current->children[c] = new TrieNode(); } current->children[c]->count += 1; current = current->children[c]; } } vector<vector<char>> char_cur(3000, vector<char>(26)); int calc(const string& t, TrieNode* root, int i) { if (i == t.size()) { return root->count; } char c = t[i]; int res = 0; for (auto [d, child] : root->children) { if (char_cur[i][d - 'a'] == c) { res += calc(t, child, i + 1); } } return res; } int main() { fast_io(); int n, l, q; cin >> n >> l >> q; vector<string> s(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } TrieNode* tree = new TrieNode(); for (const string& t : s) { insert(tree, t); } for (int i = 0; i < l; ++i) { for (int j = 0; j < 26; ++j) { char_cur[i][j] = 'a' + j; } } for (int i = 0; i < q; ++i) { int com; cin >> com; if (com == 1) { int k; char c, d; cin >> k >> c >> d; --k; replace(char_cur[k].begin(), char_cur[k].end(), c, d); } else { string t; cin >> t; cout << calc(t, tree, 0) << "\n"; } } return 0; }