测试点3建议样例:
3
aaasssccc
测试点2,4:注意输出可能坏键的字符顺序是按照字符串中出现的顺序。
下面讲讲我的思路:
设置map<char, bool > map1;存储出现过的字符,false表示不是坏键,true表示是坏键。
然后处理字符串,字符每次和前一个比较,如果一样就len++。不一样就看map1中有没有出现过这个字符:
1.如果map1中有,并且为false,说明之前就出现了该字符,并且不是坏键。
3
eeeesseeeaaa
//扫描到最后一个e的时候,len==num,但是这不是坏键。因为之前已经出现连续4个e,所以这就是第一种情况;
2.map1中有,并且为true,不需要处理。
3.map1中没有,说明第一次出现,可能使坏键,设置成true;
当循环结束后,可能是测试点3那种情况。要判断最后一个字符加上去之后是不是len==num了。如果相等还要在进行判断最后出现的字符是不是坏键。
map2设置为了按字符出现顺序输出坏键。
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <memory.h>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int pos = 1, num, len = 1;
cin >> num;
string s;
getchar();
getline(cin, s);
map<char, bool > map1;
while (pos < s.length()) {
if (s[pos] == s[pos - 1]){
len++;
}
else {
if (len%num != 0) {
map1[s[pos-1]] = false;
}
else {
if (map1.find(s[pos - 1])==map1.end()) {
map1[s[pos-1]] = true;
}
}
len = 1;
}
pos++;
}
if (len%num != 0) {
map1[s[pos-1]] = false;
}
else {
if (map1.find(s[pos-1]) == map1.end()) {
map1[s[pos-1]] = true;
}
}
map<char, bool > map2 = map1;
for (int i = 0; i < s.length(); i++) {
if (map2[s[i]] == true) {
cout << s[i];
map2[s[i]] = false;
}
}
cout << endl;
for (int i = 0; i < s.length(); i++) {
if (map1[s[i]]== true) {
i = i + num- 1;
}
cout << s[i];
}
return 0;
}