th0m45s5helby

Untitled

Aug 29th, 2022
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string solve(string s , int r)
  5. {
  6.  
  7.     if (r == 1) return "";
  8.    
  9.     string ans = "";
  10.  
  11.     stack<pair<char, int> > stack;
  12.  
  13.     for (int i = 0; i < s.length(); i++) {
  14.  
  15.         if (stack.empty()) stack.push({s[i], 1});
  16.        
  17.         else {
  18.  
  19.             if (s[i] == stack.top().first) {
  20.                
  21.                 stack.push( { s[i], stack.top().second + 1 } );
  22.                
  23.                 if (stack.top().second == r) {
  24.                     int temp = r;
  25.                     while (temp) {
  26.                         stack.pop();
  27.                         temp--;
  28.                     }
  29.                 }
  30.             }
  31.             else  stack.push({s[i], 1});
  32.            
  33.         }
  34.     }
  35.  
  36.     while (!stack.empty()) {
  37.         ans += stack.top().first;
  38.         stack.pop();
  39.     }
  40.     reverse(ans.begin(), ans.end());
  41.     return ans;
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.  
  48.     string s;
  49.     cin>>s;
  50.     int r;
  51.     cin>>r;
  52.     cout << solve(s,r);
  53.  
  54.     return 0;
  55. }
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment