Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- int main()
- {
- std::vector<std::string> compressed;
- std::string input;
- int counter = 1;
- std::cin >> input;
- for (int element = 0; element < input.size(); element++)
- {
- std::string formattedInput;
- if (input[element] == input[element + 1] && input[element + 1] != input.size()+1)
- {
- counter++;
- }
- else if (input[element] != input[element + 1] && counter == 1)
- {
- formattedInput += input[element];
- compressed.push_back(formattedInput);
- counter = 1;
- }
- else if (input[element] != input[element + 1] && counter == 2)
- {
- formattedInput += input[element];
- formattedInput += input[element];
- compressed.push_back(formattedInput);
- counter = 1;
- }
- else if (input[element] != input[element + 1] && counter > 2)
- {
- std::stringstream ss;
- ss << counter;
- formattedInput += ss.str();
- formattedInput += input[element];
- compressed.push_back(formattedInput);
- counter = 1;
- }
- }
- for (auto i : compressed)
- {
- std::cout << i;
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement