#include #include #include using namespace std; string copyingText (string text, int copyFrom, int copyTo) { string result; int copyBeginIndex = 0; int copyEndIndex = 0; for (int i = copyFrom; i >= 0; --i) { if (text [i] == ' ') { copyBeginIndex = i + 1; for (int k = copyTo; k <= (text.size() - 1); ++k) { if (text [k] == ' ') { copyEndIndex = k; break; } if (k + 1 > (text.size() - 1)) { copyEndIndex = text.size(); break; } } if (copyTo > (text.size() - 1)) { copyEndIndex = text.size(); break; } } if (i == 0) { copyBeginIndex = 0; for (int k = copyTo; k <= (text.size() - 1); ++k) { if (text [k] == ' ') { copyEndIndex = k; break; } if (k + 1 == (text.size() - 1)) { copyEndIndex = text.size(); break; } } if (copyTo > (text.size() - 1)) { copyEndIndex = text.size(); break; } } } result = text.substr (copyBeginIndex, copyEndIndex); return result; } string pastedText (string text, string copiedText, int pasteIn) { string result; if (text [pasteIn] == ' ') { result = text.substr (0, pasteIn); result.append (" "); result.append (copiedText); result += text.substr (pasteIn, text.size()); } else { result = text.substr (0, pasteIn); result.append (copiedText); result.append (text.substr(pasteIn, text.size())); } return result; } int main() { string text; getline (cin, text); string command; int copyFrom, copyTo, pasteIn; stack copiedText; cycle:while (command != "end") { cin >> command; if (command == "copy") { cin >> copyFrom; cin >> copyTo; string copyText = copyingText(text, copyFrom, copyTo); copiedText.push (copyText); } else if (command == "paste") { if (copiedText.empty()) { goto cycle; } else { cin >> pasteIn; string copyText = copiedText.top(); copiedText.pop(); text = pastedText (text, copyText, pasteIn); } } } cout << text; return 0; }