fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mod 1000000007
  4. #define ll long long
  5. #define faster() cin.tie(0); ios_base::sync_with_stdio(false); cout.tie(0);
  6. string xuly(string s, int k){
  7. stack<pair<char,int>> st;
  8. for(int i = 0 ; i < s.size() ; i++){
  9. if(!st.empty() && s[i] == st.top().first && st.top().second == k - 1){
  10. st.pop();
  11. }
  12. else{
  13. if(st.empty() || s[i] != st.top().first) st.push({s[i],1});
  14. else st.top().second++;
  15. }
  16. }
  17. string res = "";
  18. while(!st.empty()){
  19. auto it = st.top();st.pop();
  20. for(int i = 0 ; i < it.second ; i++){
  21. res += it.first;
  22. }
  23. }
  24. reverse(res.begin(),res.end());
  25. return res;
  26. }
  27. int main(){
  28. faster()
  29. string s;
  30. cin >> s;
  31. int k ;cin >> k;
  32. if(xuly(s,k) == "") cout << "empty\n";
  33. else{
  34. cout << xuly(s,k);
  35. }
  36. }
  37.  
  38.  
Success #stdin #stdout 0.01s 5256KB
stdin
aabbccedde
2
stdout
empty