Skip to content

Instantly share code, notes, and snippets.

@shinchit
Created September 30, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/32ae487305ceee870125407021718ddf to your computer and use it in GitHub Desktop.
Save shinchit/32ae487305ceee870125407021718ddf to your computer and use it in GitHub Desktop.
/*
* This code is also a solution to the problem at the following URL.
* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_9_D
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Command {
string func;
int a;
int b;
string p;
};
int main() {
string str;
cin >> str;
int q;
cin >> q;
vector<Command> commands;
for ( int i = 0; i < q; i++ ) {
string func;
cin >> func;
int a, b;
cin >> a >> b;
string p;
if ( func == "replace" ) {
cin >> p;
}
Command command = {func, a, b, p};
commands.push_back(command);
}
for ( int i = 0; i < q; i++ ) {
Command command = commands[i];
if ( command.func == "print" ) {
for ( int j = command.a; j < command.b+1; j++) {
cout << str[j];
}
cout << endl;
} else if ( command.func == "reverse" ) {
reverse(str.begin()+command.a, str.begin()+command.b+1);
} else if ( command.func == "replace" ) {
str.replace(command.a, command.b-command.a+1, command.p);
} else {
// nothing done.
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment