Advertisement
Guest User

RLE code

a guest
Aug 20th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. string my_to_string(int n){
  6.     string ans;
  7.     while( n > 0 ){
  8.         ans += (n % 10 + '0');
  9.         n /= 10;
  10.     }
  11.     reverse(ans.begin(), ans.end());
  12.     return ans;
  13. }
  14.  
  15. string decode(string code){
  16.     int p = 0;
  17.     string ans, tmp;
  18.     while( p < code.size() ){
  19.         if(code[ p ] >= 'a' and code[ p ] <= 'z'){
  20.             int ile = stoi(tmp);
  21.             while( ile -- ) ans += code[ p ];
  22.             tmp = "";
  23.         }
  24.         else{
  25.             tmp += code[ p ];
  26.         }
  27.         p ++;
  28.     }
  29.     return ans;
  30. }
  31.  
  32. int main()
  33. {
  34.    
  35.     string s;
  36.     cin >> s;
  37.     string ans;
  38.     int p = 0;
  39.     while( p < s.size() ){
  40.         int k = p;
  41.         while(s[ p ] == s[ k ]){
  42.             k ++;
  43.         }
  44.         int ile = k - p;
  45.         ans += my_to_string( ile );
  46.         ans += s[ p ];
  47.         p = k;
  48.     }
  49.     cout << ans << endl;
  50.     cout << decode( ans ) << endl;
  51.    
  52.    
  53.     cout << endl;
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement