https://www.patest.cn/contests/pat-a-practise/1112
#include <cstdio>
#include <vector>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
bool map[256];
int main()
{
vector<char> stuckKeysPre;
set<char> stuckKey;
int k, reps[1005];
char line[1005];
fill(reps, reps + 1005, 1);
fill(map, map + 256, false);
scanf("%d\n%s",&k,line);
int len = strlen(line);
for (int i = 1; i < len; i++)
{
if (line[i] == line[i - 1]) reps[i] = reps[i - 1] + 1;
if (reps[i] == k) {
map[line[i]] = true;
stuckKeysPre.push_back(line[i]);
reps[i] = 0;
}
else map[line[i]] = false;
}
//check the mess up for example , k=3 ee^&((*eee*^*eee. `e` is not stuck key
for (int i = len-1; i >=0; )
{
if (reps[i] == 0) {
i -= k;
}else if (reps[i] < k) {
map[line[i]] = false;
i -= reps[i];
}
}
int nV = stuckKeysPre.size();
for (int i = 0; i < nV; i++)
{
if (map[(int)stuckKeysPre[i]] &&
stuckKey.end() == stuckKey.find(stuckKeysPre[i])) {//dedup
printf("%c", stuckKeysPre[i]);
stuckKey.insert(stuckKeysPre[i]);
}
}
printf("\n");
for (int i = 0; i < len; i++) {
if (map[line[i]]) i += (k-1);
printf("%c",line[i]);
}
printf("\n");
return 0;
}