cxx 题

· · 题解

使用 rope 解决此类问题。

对于 1,2 操作,使用 substr 函数将 s_{0\cdots l-1},s_{l\cdots r},s_{r+1\cdots len-1} 取出,再根据题目要求将 s_{l\cdots r} 拼贴到最前面/最后面即可。

对于 3 操作,直接访问 y 位置上的值输出即可。

对于 4 操作,因为 rope 自带可持久化,所以可以每次操作新建一个版本,查询时直接访问 x 版本中 y 位置的值即可。

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
crope s[10005];
char S[10005];
int n,l,d;
int main(){
    cin>>S;
    s[0].insert(0,S);
    scanf("%d",&n);
    int op,x,y;
    for(int i=1;i<=n;i++){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&x,&y);
            s[i]=s[i-1].substr(x,y-x+1)+s[i-1].substr(0,x)+s[i-1].substr(y+1,s[i-1].length()-y-1);
        }
        if(op==2){
            scanf("%d%d",&x,&y);
            s[i]=s[i-1].substr(0,x)+s[i-1].substr(y+1,s[i-1].length()-y-1)+s[i-1].substr(x,y-x+1);
        }
        if(op==3){
            scanf("%d",&x);
            s[i]=s[i-1];
            printf("%c\n",s[i][x]);
        }
        if(op==4){
            scanf("%d%d",&x,&y);
            s[i]=s[i-1];
            printf("%c\n",s[x][y]);
        }
    }
    return 0;
}