結果
| 問題 |
No.2761 Substitute and Search
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-31 12:55:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,558 bytes |
| コンパイル時間 | 5,704 ms |
| コンパイル使用メモリ | 282,588 KB |
| 最終ジャッジ日時 | 2025-02-21 17:18:15 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 MLE * 7 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
struct Trie{
vector<map<char,int>> node;
vector<int> cnt;
vector<set<int>> depth;
Trie(){
node.push_back(map<char,int>());
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].count(s[i]-'a') == 0){
node.push_back(map<char,int>());
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].count(s[i]-'a') == 0){
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].count(c-'a')==0)
continue;
if(node[root].count(d-'a') == 0){
depth[k+1].erase(node[root][d-'a']);
node[root][d-'a'] = node[root][c-'a'];
node[root].erase(c-'a');
}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].erase(c-'a');
while(Q.size()){
auto [a, b,k] = Q.front();Q.pop();
for(int j = 0;j<26;j++){
if(node[a].count(j)==0){
continue;
}
if(node[b].count(j) == 0){
depth[k+1].erase(node[b][j]);
node[b][j] = node[a][j];
node[a].erase(j);
}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].erase(j);
}
}
}
}
}
}
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]);
S[i].clear();
}
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;
T[i].clear();
}
}
}
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();
}