1112 Stucked Keyboard (20分)
1112 Stucked Keyboard (20分)
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
-
思路:
-
1.直接找好的键,条件是!(出现连续k次的键)
-
2.其中,判断之后,剩下的[i,x.size()-1]区间都要令为好的键!~
-
3.测试点1的用例: 3 aaab
-
4.收集坏键需要按照被detected的顺序,直接来个map exist去下重就ok拉
代码:
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int k;
string x;
map<char, int> isGood;
int main(){
cin >> k >> x;
int i,flag ; //isBad
for (i = 0; i <= x.size()-k;){ //i<=x.size()-k是为了下面判断不越界,因此后面可能多余出来的键需要再处理
flag = 1;
for (int j = 1; j < k;j++)
if(x[i+j]!=x[i])
flag = 0;
if(!flag){
isGood[x[i]] = 1;
i++;
}else{
i += k;
}
}
for (; i < x.size();i++)//可能会剩余[0,k-1]个没有遍历过的字母,这些字母不能满足连续重复k次,所以都是好键
isGood[x[i]] = 1;
vector<char> bad;
map<char, bool> exist;
for (int i = 0; i < x.size();i++){
if(!isGood[x[i]]&&!exist[x[i]]){
bad.push_back(x[i]);
exist[x[i]] = 1;
}
}
for (int i = 0; i < bad.size();i++)
cout << bad[i];
cout << endl;
for (int i = 0; i < x.size();){
cout << x[i];
i += isGood[x[i]] ? 1 : k;
}
return 0;
}