记录正常按键。在出现过的字符中,剩下的按键就是有故障的。
细节很重要。今天状态不好,写出来的代码比较丑陋。
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int main(void)
{
char str[1100], repeated[100], key;
int rear, k, i, j, cnt;
bool stuck[300] = { false }, normal[300] = { false };
vector<char> v;
vector<char>::iterator vit;
scanf("%d %s", &k, str);
key = str[0];
cnt = 1;
for (i = 1; str[i] != '\0'; i++) {
if (key == str[i]) {
cnt++;
}
else {
if (cnt % k) {
normal[key - '\0'] = true;
for (vit = v.begin(); vit != v.end(); vit++) {
if (*vit == key) {
v.erase(vit);
break;
}
}
}
else {
if (!normal[key - '\0']) {
for (vit = v.begin(); vit != v.end(); vit++) {
if (*vit == key) {
break;
}
}
if (vit == v.end()) {
v.push_back(key);
}
}
}
cnt = 1;
key = str[i];
}
}
if (cnt % k) {
normal[key - '\0'] = true;
for (vit = v.begin(); vit != v.end(); vit++) {
if (*vit == key) {
v.erase(vit);
break;
}
}
}
else {
if (!normal[key - '\0']) {
for (vit = v.begin(); vit != v.end(); vit++) {
if (*vit == key) {
break;
}
}
if (vit == v.end()) {
v.push_back(key);
}
}
}
for (vit = v.begin(); vit != v.end(); vit++) {
stuck[*vit - '\0'] = true;
}
char str2[1200];
cnt = 0;
key = 0;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (i) {
if (str[i] == key && stuck[str[i] - '\0']) {
cnt++;
if (!(cnt % k)) {
str2[j++] = str[i];
cnt = 0;
}
}
else if(str[i] != key && stuck[str[i] - '\0']) {
cnt = 1;
key = str[i];
}
else {
str2[j++] = str[i];
}
}
else if (normal[str[i] - '\0']) {
key = str[i];
cnt = 1;
str2[j++] = str[i];
}
else {
key = str[i];
cnt = 1;
}
}
str2[j] = '\0';
for (vit = v.begin(); vit != v.end(); vit++) {
putchar(*vit);
}
putchar('\n');
puts(str2);
return 0;
}