Advertisement
Josif_tepe

Untitled

Sep 23rd, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. void write_numbers(std::string &s, std::vector<int> &numbers) {
  5.     for(int i = 0; i < (int) s.size(); i++) {
  6.         int j = i, cnt = 0;
  7.         while(j < (int) s.size() and s[i] == s[j]) {
  8.             ++cnt;
  9.             ++j;
  10.         }
  11.         i = j - 1;
  12.         numbers.push_back(cnt);
  13.     }
  14. }
  15. void write_characters(std::string &s, std::vector<char> &characters) {
  16.     for(int i = 0; i < (int) s.size(); i++) {
  17.         int j = i;
  18.         char c = s[i];
  19.         while(j < (int) s.size() and s[i] == s[j]) {
  20.             ++j;
  21.         }
  22.         i = j - 1;
  23.         characters.push_back(c);
  24.     }
  25. }
  26.  
  27. void solve() {
  28.     std::string s;
  29.     std::cin >> s;
  30.    
  31.     try{
  32.         for(int i = 0; i < (int) s.size(); i++) {
  33.             if(s[i] >= 'A' and s[i] <= 'Z') {
  34.                 throw "Invalid input!";
  35.             }
  36.             if(s[i] >= '0' and s[i] <= '9') {
  37.                 throw "Invalid input!";
  38.             }
  39.            
  40.         }
  41.     }
  42.     catch(const char *c) {
  43.         std::cout << c << std::endl;
  44.         return;
  45.     }
  46.  
  47.     std::vector<int> numbers;
  48.     std::vector<char> characters;
  49.    
  50.     write_numbers(s, numbers);
  51.     write_characters(s, characters);
  52.    
  53.     for(int i = 0; i < (int) numbers.size(); i++) {
  54.         std::cout << numbers[i] << characters[i];
  55.     }
  56.     std::cout << std::endl;
  57. }
  58. int main() {
  59.     solve();
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement