結果
問題 |
No.2761 Substitute and Search
|
ユーザー |
|
提出日時 | 2024-05-31 11:13:44 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 3,730 bytes |
コンパイル時間 | 27,052 ms |
コンパイル使用メモリ | 358,576 KB |
最終ジャッジ日時 | 2025-02-21 17:12:55 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 6 MLE * 7 |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; #include<atcoder/all> using namespace atcoder; using ll = long long; struct Trie{ vector<array<int,26>> node; vector<int> cnt; vector<set<int>> depth; Trie(){ node.push_back(array<int,26>()); for(int i = 0;i<26;i++) node[0][i] = -1; cnt.push_back(0); depth.push_back(set<int>()); depth[0].insert(0); } void add(string s){ int now = 0; for(int i = 0;i<s.size();i++){ if(node[now][s[i]-'a'] == -1){ node.push_back(array<int,26>()); for(int j = 0;j<26;j++) node[node.size()-1][j] = -1; cnt.push_back(0); depth.push_back(set<int>()); node[now][s[i]-'a'] = node.size()-1; depth[i+1].insert(node.size()-1); } now = node[now][s[i]-'a']; cnt[now]++; } } int get_cnt(string s){ int now = 0; for(int i = 0;i<s.size();i++){ if(node[now][s[i]-'a'] == -1){ return 0; } now = node[now][s[i]-'a']; } return cnt[now]; } void swap(int k, char c, char d){ for(auto root :depth[k]){ if(node[root][c-'a'] == -1) continue; if(node[root][d-'a'] == -1){ depth[k+1].erase(node[root][d-'a']); node[root][d-'a'] = node[root][c-'a']; node[root][c-'a'] = -1; }else{ queue<tuple<int,int,int>> Q; Q.push({node[root][c-'a'], node[root][d-'a'],k+1}); depth[k+1].erase(node[root][c-'a']); cnt[node[root][d-'a']] += cnt[node[root][c-'a']]; cnt[node[root][c-'a']] = 0; node[root][c-'a'] = -1; while(Q.size()){ auto [a, b,k] = Q.front();Q.pop(); for(int j = 0;j<26;j++){ if(node[a][j]==-1){ continue; } if(node[b][j] == -1){ depth[k+1].erase(node[b][j]); node[b][j] = node[a][j]; node[a][j] = -1; }else{ Q.push({node[a][j], node[b][j],k+1}); depth[k+1].erase(node[a][j]); cnt[node[b][j]] += cnt[node[a][j]]; cnt[node[a][j]] = 0; node[a][j] =-1; } } } } } } void print(){ for(int i = 0;i<node.size();i++){ cout<<i<<": "; for(int j = 0;j<26;j++){ if(node[i][j] != -1){ cout<<char(j+'a')<<node[i][j]<<' '; } } cout<<endl; } /* cout<<"depth: "<<endl; for(int i = 0;i<depth.size();i++){ cout<<i<<": "; for(auto x:depth[i]){ cout<<x<<' '; } cout<<endl; } */ cout<<"cnt:"<<endl; for(int i = 0;i<cnt.size();i++){ cout<<i<<": "<<cnt[i]<<endl; } } }; int N,L,Q; vector<string> S; vector<int> OP; vector<string> T; vector<int> K; vector<char> C,D; void solve(){ Trie trie; for(int i = 0;i<N;i++){ trie.add(S[i]); } cerr<<trie.node.size()<<endl; //trie.print(); for(int i = 0;i<Q;i++){ if(OP[i] == 1){ trie.swap(K[i]-1, C[i], D[i]); //trie.print(); }else{ cout << trie.get_cnt(T[i]) << endl; } } } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N >> L >> Q; S.resize(N); OP.resize(Q); T.resize(Q); K.resize(Q); C.resize(Q); D.resize(Q); for(int i = 0;i<N;i++){ cin >> S[i]; } for(int i = 0;i<Q;i++){ cin >> OP[i]; if(OP[i] == 2){ cin >> T[i]; }else{ cin >> K[i] >> C[i] >> D[i]; } } solve(); }