Advertisement
bobo_bobkata

Untitled

Nov 5th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <sstream>
  5. #include <cmath>
  6. #include <list>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. int getLastIndexToCopy(string line, int index);
  16.  
  17. int getFirstIndexToCopy(string line, int index);
  18.  
  19.  
  20. int main() {
  21. string line;
  22. std::getline(cin, line);
  23. stack<string> copied;
  24. while (true) {
  25. string input;
  26. std::getline(cin, input);
  27. if (input == "end") {
  28. break;
  29. }
  30. stringstream stream(input);
  31. vector<string> splited;
  32. string str = "";
  33. while (stream >> str) {
  34. splited.push_back(str);
  35. }
  36. string command = splited[0];
  37. if (command == "copy") {
  38. int from = stoi(splited[1]);
  39. int to = stoi(splited[2]);
  40. int start = getFirstIndexToCopy(line, from);
  41. int end = getLastIndexToCopy(line, to);
  42. string substringed = line.substr(start, end - start);
  43. copied.push(substringed);
  44. } else if (command == "paste" && !copied.empty()) {
  45. int index = stoi(splited[1]);
  46. string before = line.substr(0, index);
  47. string after = line.substr(index, line.length());
  48. string current = copied.top();
  49. copied.pop();
  50. if (line[index] == ' ') {
  51. if (before[before.length() - 1] != ' ') {
  52. current = " " + current;
  53. }
  54. if (after[0] != ' ') {
  55. current = current + " ";
  56. }
  57. }
  58. line = (before + current + after);
  59. }
  60.  
  61. }
  62. std::cout << line << std::endl;
  63.  
  64. return 0;
  65. }
  66.  
  67. int getFirstIndexToCopy(string line, int index) {
  68. if (index < 0) {
  69. return 0;
  70. }
  71. int firstIndex = index;
  72. for (int i = index; i >= 0; --i) {
  73. if (line[i] == ' ') {
  74. break;
  75. }
  76. firstIndex = i;
  77. }
  78. if (line[firstIndex] == ' ') {
  79. firstIndex++;
  80. }
  81. return firstIndex;
  82. }
  83.  
  84.  
  85. int getLastIndexToCopy(string line, int index) {
  86. int lastIndex = line.length();
  87. for (int i = index; i < line.length(); ++i) {
  88. if (line[i] == ' ') {
  89. lastIndex = i;
  90. break;
  91. }
  92. }
  93. return lastIndex;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement