Interview Question
Country: United States
//g++ 7.4.0
#include <bits/stdc++.h>
using namespace std;
int main()
{
string c;
cin >> c;
int k;
cin >> k;
stack<pair<char, int> > s;
int n = c.length();
for (int i=0; i<n; ++i) {
if (s.empty()) s.push(make_pair(c[i], 1));
else {
if (s.top().first == c[i] && s.top().second == k-1) {
while (!s.empty() && s.top().first == c[i]) s.pop();
} else if (s.top().first == c[i]) {
s.push(make_pair(c[i], s.top().second+1));
} else {
s.push(make_pair(c[i], 1));
}
}
}
int m = s.size();
char res[m];
for (int i=m-1; i>=0; --i) {
res[i] = s.top().first; s.pop();
}
for (int i=0; i<m; ++i) {
cout << res[i];
}
return 0;
}
Well, well. I am not entirely sure... but this seems to fit the bill. It was fun.
- NoOne April 27, 2020Can anyone break it?