题解:UVA12538 自带版本控制功能的IDE Version Controlled IDE

· · 题解

这是一道可癌的水题 rope 题。

前置知识

思路

直接使用 rope 模拟,因为 rope 底层其实是可持久化平衡树,所以可以直接用新版本 = 旧版本加操作就可以了。

Code

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
int Q,id,opt,p,v,c,cnt;
rope<char> str[50005],lst,tmp;
string s;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> Q;
    while(Q --){
        cin >> opt;
        if(opt == 1){
            cin >> p >> s;
            p -= cnt;
            lst.insert(p,s.c_str());
            str[++id] = lst;
        }else if(opt == 2){
            cin >> p >> c;
            p -= cnt,c -= cnt;
            lst.erase(p - 1,c);
            str[++id] = lst;
        }else{
            cin >> v >> p >> c;
            v -= cnt,p -= cnt,c -= cnt;
            tmp.clear();
            tmp = str[v].substr(p - 1,c);
            cnt += count(tmp.begin(),tmp.end(),'c');
            cout << tmp << '\n';
        }
    }
    return 0;
}