fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ll long long
  5. #define endl "\n"
  6. #define FOR(i, a, b) for (int i = a; i <= b; i++)
  7. #define FORD(i, a, b) for (int i = a; i > b; i--)
  8. #define FORu(i, a, b) for (int i = a; i < b; i++)
  9. #define vii vector <int>
  10. #define pb push_back
  11. #define fi first
  12. #define se second
  13. #define sz(a) (int)a.size()
  14. #define all(x) x.begin(), x.end()
  15. #define filei "BAI4.INP"
  16. #define fileo "BAI4.out"
  17. int main()
  18. {
  19. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  20. if (fopen(filei, "r"))
  21. {
  22. freopen(filei, "r", stdin);
  23. freopen(fileo, "w", stdout);
  24. }
  25. int n, k; string s;
  26. cin >> n >> k >> s;
  27. stack < pair <char, int> > st;
  28. FORu(i, 0, sz(s))
  29. {
  30. if (st.empty()) st.push({s[i], 1});
  31. else
  32. {
  33. if (st.top().fi == s[i])
  34. {
  35. ++st.top().se;
  36. if (st.top().se == k) st.pop();
  37. }
  38. else
  39. {
  40. st.push({s[i], 1});
  41. }
  42. }
  43. }
  44. string c = "";
  45. while (!st.empty())
  46. {
  47. c = string(st.top().se, st.top().fi) + c;
  48. st.pop();
  49. }
  50. cout << c;
  51.  
  52.  
  53. }
Success #stdin #stdout 0.01s 5316KB
stdin
12 3
qddxxxdqqhhhhq
stdout
hq