http://www.sgi.com/tech/stl/Rope.html
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 #include <ext/rope> 15 16 using namespace std; 17 using namespace __gnu_cxx; 18 19 #define REP(i, n) for (int i = 0; i < (n); ++i) 20 #define eps 1e-9 21 #define SZ(x) ((int)x.size()) 22 #define PI acos(-1.0) 23 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 const int maxn = 2e6 + 10; 27 crope my_rope; 28 char s[maxn], opt[15]; 29 int q, p, n; 30 char c; 31 32 int main() { 33 #ifdef __AiR_H 34 freopen("in.txt", "r", stdin); 35 // freopen("out.txt", "w", stdout); 36 #endif // __AiR_H 37 scanf("%d", &q); 38 while (q--) { 39 scanf("%s", opt); 40 if (opt[0] == 'M') { scanf("%d", &p); } 41 else if (opt[0] == 'I') { 42 scanf("%d", &n); 43 for (int i = 0; i < n;) { 44 c = getchar(); 45 if (32 <= c && c <= 126) { s[i++] = c; } 46 } 47 s[n] = '\0'; my_rope.insert(p, s); 48 } else if (opt[0] == 'D') { 49 scanf("%d", &n); my_rope.erase(p, n); 50 } else if (opt[0] == 'G') { 51 scanf("%d", &n); my_rope.copy(p, n, s); 52 s[n] = '\0'; printf("%s\n", s); 53 } else if (opt[0] == 'P') { --p; } 54 else { ++p; } 55 } 56 return 0; 57 }
2 BZOJ 1269 [AHOI2006] 文本编辑器editor
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 #include <ext/rope> 15 16 using namespace std; 17 using namespace __gnu_cxx; 18 19 #define REP(i, n) for (int i = 0; i < (n); ++i) 20 #define eps 1e-9 21 #define SZ(x) ((int)x.size()) 22 #define PI acos(-1.0) 23 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 const int maxn = 2e6 + 10; 27 crope rope1, rope2, t; 28 char opt[20], s[maxn]; 29 char c; 30 int q, p, n, len; 31 32 int main() { 33 #ifdef __AiR_H 34 freopen("in.txt", "r", stdin); 35 // freopen("out.txt", "w", stdout); 36 #endif // __AiR_H 37 scanf("%d", &q); 38 while (q--) { 39 scanf("%s", opt); 40 if (opt[0] == 'M') { scanf("%d", &p); } 41 else if (opt[0] == 'I') { 42 scanf("%d", &n); len = rope1.length(); 43 for (int i = 0; i < n;) { 44 c = getchar(); 45 if (32 <= c && c <= 126) { s[i++] = c; } 46 } 47 s[n] = '\0'; rope1.insert(p, s); 48 reverse(s, s + n); rope2.insert(len - p, s); 49 } else if (opt[0] == 'D') { 50 scanf("%d", &n); len = rope1.length(); 51 rope1.erase(p, n); rope2.erase(len - p - n, n); 52 } else if (opt[0] == 'G') { printf("%c\n", rope1[p]); } 53 else if (opt[0] == 'P') { --p; } 54 else if (opt[0] == 'N') { ++p; } 55 else { 56 scanf("%d", &n); len = rope1.length(); 57 t = rope1.substr(p, n); 58 rope1 = rope1.substr(0, p) + rope2.substr(len - p - n, n) + rope1.substr(p + n, len - p - n); 59 rope2 = rope2.substr(0, len - p - n) + t + rope2.substr(len - p, p); 60 } 61 } 62 return 0; 63 }