Advertisement
a53

treasure

a53
Nov 18th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. stack < pair < char, int > > a;
  4. int n,k,i,nr;
  5. char s[200004],b[200004];
  6.  
  7. int main()
  8. {
  9. scanf ("%d %d", &n, &k);
  10. assert ((1<=n) && (n<=200000));
  11. assert ((1<=k) && (k<=200000));
  12. scanf ("%s", &s);
  13. for (i=0;i<=(n-1);i++)
  14. assert (('a'<=s[i]) && (s[i]<='z'));
  15. for (i=0;i<n;i++)
  16. {
  17. if (a.empty())
  18. a.push({s[i],1});
  19. else if (s[i]==a.top().first)
  20. {
  21. a.top().second++;
  22. if (a.top().second==k)
  23. a.pop();
  24. }
  25. else
  26. a.push({s[i],1});
  27. }
  28. nr=0;
  29. while (!a.empty())
  30. {
  31. for (i=1;i<=a.top().second;i++)
  32. b[++nr]=a.top().first;
  33. a.pop();
  34. }
  35. reverse(b+1,b+nr+1);
  36. for (i=1;i<=nr;i++)
  37. printf ("%c", b[i]);
  38. printf ("\n");
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement