Rope大法(可持久化平衡树)

2008年OI集训论文上有介绍<对块状链表的一点研究>,其主要是结合了链表和数组各自的优点,链表中的节点指向每个数据

块,即数组,并且记录数据的个数,然后分块查找和插入。在g++头文件中,<ext/rope>中有成型的块状链表,在using namespace 

__gnu_cxx;空间中,其操作十分方便。

  基本操作:

rope test;

test.push_back(x);//在末尾添加x

test.insert(pos,x);//在pos插入x  

test.erase(pos,x);//从pos开始删除x个

test.copy(pos,len,x);//从pos开始到pos+len为止用x代替

test.replace(pos,x);//从pos开始换成x

test.substr(pos,x);//提取pos开始x个

test.at(x)/[x];//访问第x个元素

其算法复杂度n*(n^0.5),可以在很短的时间内实现快速的插入、删除和查找字符串,是一个很厉害的神器!

这里有英文版的详解:http://www.sgi.com/tech/stl/Rope.html

测试代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <ext/rope>
#include <algorithm>
using namespace std;
using __gnu_cxx::crope;

const int maxn = 10010;
char op[maxn];
crope text;

int main(){
    text.clear();
    scanf("%s",op);
    int l = strlen(op);
    for(int i = 0; i < l; i++)
        text += op[i];
    cout<<text<<endl;

    cout<<"test.push_back(x);//在末尾添加x"<<endl;
    text.push_back('a');
    cout<<text<<endl;
    /*
    text.push_back("bbb");
    cout<<text<<endl;//编译错误
    */

    cout<<"test.insert(pos,x);//在pos插入x"<<endl;
    text.insert(1,'b');
    cout<<text<<endl;
    text.insert(2,"aaa");
    cout<<text<<endl<<endl;

    cout<<"test.erase(pos,x);//从pos开始删除x个"<<endl;
    text.erase(1,3);
    cout<<text<<endl<<endl;

    cout<<"test.copy(pos,len,x);//从pos开始到pos+len为止用x代替"<<endl;
    text.copy(1,5,op);
    cout<<text<<endl<<endl;

    cout<<"test.replace(pos,x);//从pos开始换成x"<<endl;
    text.replace(1,'c');
    cout<<text<<endl;
    text.replace(1,"ccc");
    cout<<text<<endl<<endl<<endl;

    cout<<"test.substr(pos,x);//提取pos开始x个"<<endl;
    //text = text.substr(2);这样默认为提取一个
    cout<<text.substr(2)<<endl;
    cout<<text.substr(2,3)<<endl<<endl;

    cout<<"test.at(x)/[x];//访问第x个元素"<<endl;
    cout<<text.at(4)<<endl<<endl;
}


  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值