Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <iterator>
- #include <sstream>
- #include <cmath>
- #include <list>
- #include <map>
- #include <stack>
- #include <queue>
- #include <set>
- #include <algorithm>
- using namespace std;
- int getLastIndexToCopy(string line, int index);
- int getFirstIndexToCopy(string line, int index);
- int main() {
- string line;
- std::getline(cin, line);
- stack<string> copied;
- while (true) {
- string input;
- std::getline(cin, input);
- if (input == "end") {
- break;
- }
- stringstream stream(input);
- vector<string> splited;
- string str = "";
- while (stream >> str) {
- splited.push_back(str);
- }
- string command = splited[0];
- if (command == "copy") {
- int from = stoi(splited[1]);
- int to = stoi(splited[2]);
- int start = getFirstIndexToCopy(line, from);
- int end = getLastIndexToCopy(line, to);
- string substringed = line.substr(start, end - start);
- copied.push(substringed);
- } else if (command == "paste" && !copied.empty()) {
- int index = stoi(splited[1]);
- string before = line.substr(0, index);
- string after = line.substr(index, line.length());
- string current = copied.top();
- copied.pop();
- if (line[index] == ' ') {
- if (before[before.length() - 1] != ' ') {
- current = " " + current;
- }
- if (after[0] != ' ') {
- current = current + " ";
- }
- }
- line = (before + current + after);
- }
- }
- std::cout << line << std::endl;
- return 0;
- }
- int getFirstIndexToCopy(string line, int index) {
- if (index < 0) {
- return 0;
- }
- int firstIndex = index;
- for (int i = index; i >= 0; --i) {
- if (line[i] == ' ') {
- break;
- }
- firstIndex = i;
- }
- if (line[firstIndex] == ' ') {
- firstIndex++;
- }
- return firstIndex;
- }
- int getLastIndexToCopy(string line, int index) {
- int lastIndex = line.length();
- for (int i = index; i < line.length(); ++i) {
- if (line[i] == ' ') {
- lastIndex = i;
- break;
- }
- }
- return lastIndex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement