rope 学习笔记
头文件:#include <ext/rope>(不在 bits/stdc++.h 中,万能头需使用 #include<bits/extc++.h>。
命名空间:__gnu_cxx(什么玩意儿)
| 序号 | 操作 | 语法 | 时间复杂度(定义 为 rope 大小或数组长度 | 例子 |
|---|---|---|---|---|
| 1 | 新建一个 rope | rope<类型>名称; | rope<int>q; | |
| 2 | 数组强转 rope(tips:rope 整体赋值给另一个 rope 只需要 的时间) | 名称=rope<类型>(数组名) | q=rope<int>(a); | |
| 3 | 在末尾添加元素 | 名称.push_back(元素) | q.push_back(x) | |
| 4 | 在某一下标后面添加元素 | 名称.insert(下标,元素) | q.insert(p,x) | |
| 5 | 从某一下标开始删除 个元素 | 名称.erase(下标,元素个数) | q.erase(p,x) | |
| 6 | 从某一下标开始截取 个元素 | 名称.substr(下标,元素个数) | q.substr(p,x) | |
| 7 | 访问某一元素 | 名称[下标] 或 名称.at(下标) | q[x] 或 q.at(x) | |
| 8 | 访问大小 | 名称.size() | q.size() | |
| 9 | 是否为空 | 名称.empty() | q.empty() | |
| 10 | 清空 rope | 名称.clear() | q.clear() | |
| 11 | 返回首指针 | 名称.begin() | q.begin() | |
| 12 | 返回末尾指针后一位 | 名称.end() | q.end() |
来看一道例题。
高级打字机
题目描述
早苗入手了最新的高级打字机。最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧。 请为这种高级打字机设计一个程序,支持如下 种操作:
T x:Type 操作,表示在文章末尾打下一个小写字母 。U x:Undo 操作,表示撤销最后的 次修改操作。Q x:Query 操作,表示询问当前文章中第 个字母并输出。请注意 Query 操作并不算修改操作。 文章一开始可以视为空串。
Code
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int maxn=1e6+5;
int cnt,n;
rope<char>*q[maxn];
int main(){
cin>>n;
q[0]=new rope<char>();
for(int i=1;i<=n;i++){
char opt,c;
int x;
cin>>opt;
if(opt=='T'){
cin>>c;
q[cnt]=new rope<char>(*q[++cnt-1]);
q[cnt]->push_back(c);
}
if(opt=='U'){
cin>>x;
q[cnt]=new rope<char>(*q[++cnt-x-1]);
}
if(opt=='Q'){
cin>>x;
cout<<q[cnt]->at(x-1)<<"\n";
}
}
return 0;
}