Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string solve(string s , int r)
- {
- if (r == 1) return "";
- string ans = "";
- stack<pair<char, int> > stack;
- for (int i = 0; i < s.length(); i++) {
- if (stack.empty()) stack.push({s[i], 1});
- else {
- if (s[i] == stack.top().first) {
- stack.push( { s[i], stack.top().second + 1 } );
- if (stack.top().second == r) {
- int temp = r;
- while (temp) {
- stack.pop();
- temp--;
- }
- }
- }
- else stack.push({s[i], 1});
- }
- }
- while (!stack.empty()) {
- ans += stack.top().first;
- stack.pop();
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- int main()
- {
- string s;
- cin>>s;
- int r;
- cin>>r;
- cout << solve(s,r);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment