# 1112. Stucked Keyboard (20)-PAT甲级真题

On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.

Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.

Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string “thiiis iiisss a teeeeeest” we know that the keys “i” and “e” might be stucked, but “s” is not even though it appears repeatedly sometimes. The original string could be “this isss a teest”.

#### Input Specification:

Each input file contains one test case. For each case, the 1st line gives a positive integer k ( 1<k<=100 ) which is the output repeating times of a stucked key. The 2nd line contains the resulting string on screen, which consists of no more than 1000 characters from {a-z}, {0-9} and “_”. It is guaranteed that the string is non-empty.

#### Output Specification:

For each test case, print in one line the possible stucked keys, in the order of being detected. Make sure that each key is printed once only. Then in the next line print the original string. It is guaranteed that there is at least one stucked key.

#### Sample Input:

3
caseee1__thiiis_iiisss_a_teeeeeest

#### Sample Output:

ei
case1__this_isss_a_teest

#### 分析

• 用map记录每个键是否坏，用set记录坏键盘
• 寻找坏键：对当前字符进行计数cnt，当cnt%k==0时，表示可能是坏键，map[key]=1。若cnt%k!=0，则说明该键一定不是坏键，map[key]=-1。map[key]=-1时，不能更改为map[key]=1

#include
#include
#include

using namespace std;

int main() {
int k, cnt = 1;
string s;
map m;
cin >> k;
cin >> s;
s = s + "#";
char pre = '#';
for (int i = 0; i < s.length(); i++) {
if (s[i] == pre) {
cnt++;
} else {
if (cnt % k == 0) {
if (m.find(s[i - 1]) == m.end() || m[s[i - 1]] != 0)
m[s[i - 1]] = 1;
} else {
m[s[i - 1]] = 0;
}
cnt = 1;
}
pre = s[i];
}
//    for (auto it = m.begin(); it != m.end(); it++) {
//        if (it->second != 0)
//            cout << it->first;
//    }
set printed;
for (int i = 0; i < s.length() - 1; i++) {
if (m.find(s[i]) != m.end() && m[s[i]] == 1 && printed.find(s[i]) == printed.end()) {
cout << s[i];
printed.insert(s[i]);
}
}
cout << endl;
for (int i = 0; i < s.length() - 1; i++) {
cout << s[i];
if (m.find(s[i]) != m.end() && m[s[i]] == 1)
i = i + k - 1;
}
}