Advertisement
Gistrec

Replace repeating numbers

Jan 1st, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. /**
  2.  * The program replace repeating numbers to two values - value and count of repeating
  3.  *
  4.  *
  5.  * Source array: 84 84 84 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80
  6.  *
  7.  * From array: 84 84 84 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80
  8.  * Remove from position 0 (value 84) to position 2 (value 84)
  9.  * 84 -2 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80
  10.  *
  11.  * From array: 84 -2 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80
  12.  * Remove from position 6 (value 79) to position 11 (value 79)
  13.  * 84 -2 101 115 116 32 79 -5 80 80 80 80 80
  14.  *
  15.  * From array: 84 -2 101 115 116 32 79 -5 80 80 80 80 80
  16.  * Remove from position 8 (value 80) to position 12 (value 80)
  17.  * 84 -2 101 115 116 32 79 -5 80 -4
  18.  
  19.  * Result array: 84 -2 101 115 116 32 79 -5 80 -4
  20.  * Replacements:
  21.  * 79 was replaced 5 time(s)
  22.  * 80 was replaced 4 time(s)
  23.  * 84 was replaced 2 time(s)
  24.  */
  25.  
  26. #include <vector>
  27. #include <iostream>
  28. #include <map>
  29.  
  30. using std::vector;
  31. using std::map;
  32. using std::cout;
  33. using std::endl;
  34.  
  35. map<int, int> trim_array(vector<int>& arr) {
  36.     map<int, int> replacements;
  37.  
  38.     for (size_t i = 0; i < arr.size() - 1; ++i) {
  39.         size_t from = i; // С  какого индекса удалять
  40.         size_t to   = i; // До какого индекса удалять
  41.  
  42.         // Ищем до какого индекса удалять
  43.         while (to + 1 < arr.size() && arr[from] == arr[to + 1]) to++;
  44.  
  45.         if (from != to) {
  46.             cout << "From array: ";
  47.             for (auto value : arr) cout << value << " ";
  48.             cout << endl << "Remove from position " << from << " (value " << arr[from] << ") to position " << to << " (value " << arr[to] << ")" << endl;
  49.  
  50.             const int replaceValue = arr[from];
  51.             const int replaceCount = to - from;
  52.  
  53.             // Заменяли ли мы это число
  54.             if (replacements.find(replaceValue) != replacements.end()) replacements[replaceValue] += replaceCount;
  55.             else replacements[replaceValue] = replaceCount;
  56.  
  57.             arr.erase(arr.begin() + from, arr.begin() + to);
  58.             arr.insert(arr.begin() + from + 1, -replaceCount);
  59.             i++;
  60.  
  61.             for (auto value : arr) cout << value << " ";
  62.             cout << endl << endl;
  63.         }
  64.     }
  65.     return replacements;
  66. }
  67.  
  68. int main() {
  69.     vector<int> arr{ 84, 84, 84, 101, 115, 116, 32, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80 };
  70.  
  71.     cout << "Source array: ";
  72.     for (auto value : arr) cout << value << " ";
  73.     cout << endl;
  74.  
  75.     auto replacements = trim_array(arr);
  76.  
  77.     cout << "Result array: ";
  78.     for (auto value : arr) cout << value << " ";
  79.     cout << endl;
  80.  
  81.     cout << "Replacements:" << endl;
  82.     for (const auto& replacement : replacements) {
  83.         cout << replacement.first << " was replaced " << replacement.second << " time(s)" << endl;
  84.     }
  85.  
  86.     system("pause");
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement