Rope Trick
Problem:
Given a string s, number of operations N, operations as triples ( a, b, c ), for each moves s[ a, b ] behind the xth character after the new string, output the final string.
Analysis:
This could be easily done with binary search tree. However, if time limit is not tight, we can use rope, which yields the same time complexity.
#include <bits/stdc++.h> #include <ext/rope> using namespace std; using namespace __gnu_cxx; void solve(){ rope< char > rp; string s; cin >> s; rp = s.c_str(); int N; cin >> N; for( int i = 0; i < N; ++i ){ int ql, qr, x; cin >> ql >> qr >> x; rope< char > t = rp.substr( ql, qr - ql + 1 ); rp.erase( ql, qr - ql + 1 ); rp.insert( x, t ); } for( char ch : rp ) cout << ch; cout << endl; }