Gistrec icon

Replace repeating numbers

Gistrec | PRO | 01/02/20 04:53:41 AM UTC | 0 ⭐ | 703 👁️ | Never ⏰ | []
C++ |

2.71 KB

|

None

|

0 👍

/

0 👎

/**
 * The program replace repeating numbers to two values - value and count of repeating
 *
 * 
 * Source array: 84 84 84 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80 
 * 
 * From array: 84 84 84 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80 
 * Remove from position 0 (value 84) to position 2 (value 84)
 * 84 -2 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80 
 *
 * From array: 84 -2 101 115 116 32 79 79 79 79 79 79 80 80 80 80 80 
 * Remove from position 6 (value 79) to position 11 (value 79)
 * 84 -2 101 115 116 32 79 -5 80 80 80 80 80 
 *
 * From array: 84 -2 101 115 116 32 79 -5 80 80 80 80 80 
 * Remove from position 8 (value 80) to position 12 (value 80)
 * 84 -2 101 115 116 32 79 -5 80 -4 
 
 * Result array: 84 -2 101 115 116 32 79 -5 80 -4 
 * Replacements:
 * 79 was replaced 5 time(s)
 * 80 was replaced 4 time(s)
 * 84 was replaced 2 time(s)
 */
 
#include <vector>
#include <iostream>
#include <map>
 
using std::vector;
using std::map;
using std::cout;
using std::endl;
 
map<int, int> trim_array(vector<int>& arr) {
    map<int, int> replacements;
 
    for (size_t i = 0; i < arr.size() - 1; ++i) {
        size_t from = i; // С  какого индекса удалять
        size_t to   = i; // До какого индекса удалять
 
        // Ищем до какого индекса удалять
        while (to + 1 < arr.size() && arr[from] == arr[to + 1]) to++;
 
        if (from != to) {
            cout << "From array: ";
            for (auto value : arr) cout << value << " ";
            cout << endl << "Remove from position " << from << " (value " << arr[from] << ") to position " << to << " (value " << arr[to] << ")" << endl;
 
            const int replaceValue = arr[from];
            const int replaceCount = to - from;
 
            // Заменяли ли мы это число
            if (replacements.find(replaceValue) != replacements.end()) replacements[replaceValue] += replaceCount;
            else replacements[replaceValue] = replaceCount;
 
            arr.erase(arr.begin() + from, arr.begin() + to);
            arr.insert(arr.begin() + from + 1, -replaceCount);
            i++;
 
            for (auto value : arr) cout << value << " ";
            cout << endl << endl;
        }
    }
    return replacements;
}
 
int main() {
    vector<int> arr{ 84, 84, 84, 101, 115, 116, 32, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80 };
 
    cout << "Source array: ";
    for (auto value : arr) cout << value << " ";
    cout << endl;
 
    auto replacements = trim_array(arr);
 
    cout << "Result array: ";
    for (auto value : arr) cout << value << " ";
    cout << endl;
 
    cout << "Replacements:" << endl;
    for (const auto& replacement : replacements) {
        cout << replacement.first << " was replaced " << replacement.second << " time(s)" << endl;
    }
 
    system("pause");
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎