結果
問題 |
No.2761 Substitute and Search
|
ユーザー |
|
提出日時 | 2024-05-17 22:01:46 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,274 bytes |
コンパイル時間 | 2,100 ms |
コンパイル使用メモリ | 199,848 KB |
最終ジャッジ日時 | 2025-02-21 14:50:28 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 2 TLE * 1 -- * 10 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Trie{ struct Node { std::array<int,26> child; int sz; Node(void) :sz(0) { child.fill(-1); } const int& operator[](int k) const { return child[k]; } int& operator[](int k) { return child[k]; } }; int num; vector<Node> tree; Trie() : num(0) { tree.push_back(Node()); } void insert(const string &s){ int cur = 0; for(auto c : s){ if(tree[cur][c - 'a'] == -1){ tree[cur][c - 'a'] = ++num; tree.push_back(Node()); } cur = tree[cur][c - 'a']; tree[cur].sz++; } } void erase(const string &s){ int cur = 0; for(auto c : s){ cur = tree[cur][c - 'a']; tree[cur].sz--; } } int size(const string &s){ int cur = 0; for(auto c:s){ if(tree[cur][c - 'a'] == -1) return 0; cur = tree[cur][c - 'a']; } return tree[cur].sz; } int size(int node_num){ return (node_num==-1?0:tree[node_num].sz); } int search(string &s){ int cur=0,ans=0; array<int,26> t; t.fill(0); for(auto c:s)t[c-'a']++; for(auto c:s){ for(int j=0;j<26;j++){ if(t[j])ans+=size(tree[cur][j]); } if(tree[cur][c-'a']==-1)return ans; cur=tree[cur][c-'a']; t[c-'a']--; } return ans; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, l, q; cin >> n >> l >> q; vector<string> s(n); Trie trie; for(auto &&ss : s){ cin >> ss; trie.insert(ss); } while(q--){ int cmd; cin >> cmd; if(cmd == 1){ int k; char c1, c2; cin >> k >> c1 >> c2; k--; for(auto &&ss : s){ if(ss[k] == c1){ trie.erase(ss); ss[k] = c2; trie.insert(ss); } } }else{ string s; cin >> s; cout << trie.size(s) << '\n'; } } }