rope
基本操作
頭文件
#include <ext/rope>命名空間
using namespace __gnu_cxx;定義
rope<char> c; //等價于crope c; rope<int> t; roep<Type> r;運算符 “+”:可以連接兩個rope
(rope 或 crope)
運算符 “<< ”和“ >> ”:可由輸入輸出流讀入或輸出。
c.length()/c.size() 返回長度/大小
- push_front(x); //在首部添加x
push_back(x); //在末尾添加x
- c.insert(pos, x); //在pos插入x,x可以是一個元素也可以是一個數組
c.erase(pos, x);//從pos開始刪除x個
c.copy(pos, len, x); //把c[pos~(pos+x-1)]復制到x
c.replace(pos, x); //從pos開始換成x
c.substr(pos, x); //返回一個rope類型,而且等于c[pos~(pos+x-1)],且兩者互不相干。
c.at(x)/c[x]; //訪問第x個元素
\(O(1)\)拷貝歷史版本
指針
rope<Type> *a, *b; a = new rope<Type>; b = new rope<Type> (*a);非指針
rope<Type> a, b; a = b;應用
Editor
#include <iostream> #include <cstdio> #include <cstring> #include <ext/rope>using namespace std; using namespace __gnu_cxx;const int N = 1 << 22;char ch[N]; crope text;int main() {int t, p = 0;scanf("%d", &t);while (t--) {char op[20];scanf("%s", op);if (op[0] == 'M') scanf("%d", &p);else if (op[0] == 'P') --p;else if (op[0] == 'N') ++p;else if (op[0] == 'I') {int n;scanf("%d", &n);for (int i = 0; i < n; ++i) {do scanf("%c", ch + i);while (ch[i] < 31 || 126 < ch[i]);}ch[n] = 0;text.insert(p, ch);} else if (op[0] == 'D') {int n;scanf("%d", &n);text.erase(p, n);} else {int n;scanf("%d", &n);text.copy(p, n, ch);ch[n] = 0;puts(ch);}}return 0; }文本編輯器
#include <iostream> #include <cstdio> #include <cstring> #include <ext/rope>using namespace std; using namespace __gnu_cxx;const int N = 1 << 22;char ch[N]; crope tt, rt, tmp;int main() {int t, p = 0;scanf("%d", &t);while (t--) {char op[20];scanf("%s", op);if (op[0] == 'M') scanf("%d", &p);else if (op[0] == 'P') --p;else if (op[0] == 'N') ++p;else if (op[0] == 'I') {int n;scanf("%d", &n);for (int i = 0; i < n; ++i) {do scanf("%c", ch + i);while (ch[i] < 32 || 126 < ch[i]);}ch[n] = 0;int len = tt.length();tt.insert(p, ch);reverse(ch, ch + n);rt.insert(len - p, ch);} else if (op[0] == 'D') {int n, len = tt.length();scanf("%d", &n);tt.erase(p, n);rt.erase(len - p - n, n);} else if (op[0] == 'R') {int n, len = tt.length();scanf("%d", &n);tmp = tt.substr(p, n);tt = tt.substr(0, p) + rt.substr(len - p - n, n) + tt.substr(p + n, len - p - n);rt = rt.substr(0, len - p - n) + tmp + rt.substr(len - p, p);} else if (op[0] == 'G') printf("%c\n", tt[p]);}return 0; }文藝平衡樹
#include <iostream> #include <cstdio> #include <cstring> #include <ext/rope>using namespace std; using namespace __gnu_cxx;int n, m; rope<int> s, t, tmp;int main() {scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i)s.push_back(i);for (int i = n; i; --i)t.push_back(i);while (m--) {int l, r;scanf("%d%d", &l, &r);int len = r - l + 1;tmp = s.substr(l - 1, len);s = s.substr(0, l - 1) + t.substr(n - r, len) + s.substr(r, n - r);t = t.substr(0, n - r) + tmp + t.substr(n - l + 1, l);}for (int i = 0; i < n; ++i)printf("%d ", s[i]);puts("");return 0; }轉載于:https://www.cnblogs.com/tkandi/p/9450543.html
總結
- 上一篇: Bless You Autocorrec
- 下一篇: C# Newtonsoft.Js