1112. Stucked Keyboard (20)

400 ms

65536 kB

16000 B

Standard

CHEN, Yue

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. 输出坏键时同一个坏键只能输出1次，且需按在字符串input中出现的顺序进行输出。

c++代码：

#include<bits/stdc++.h>
using namespace std;
int main(){
string input,output="";
int K;
cin>>K>>input;
int hashTable[128]={0};//表示是不是坏键
for(int i=0;i<input.size();){
int j=i+1;
while(j<input.size()&&input[j]==input[i])//找到i位置后第一个不等于input[i]的字符位置
++j;
if(hashTable[input[i]]>=0)//当前的input[i]不是坏键
hashTable[input[i]]=(j-i)%K==0?1:-1;//通过连续出现的input[i]的个数是否是K的整数倍给hashTable赋值1、-1
i=j;
}
bool out[128]={false};//表示字符是否已被输出过
for(int i=0;i<input.size();){
output+=input[i];//将当前字符加入正确的字符串中
if(hashTable[input[i]]>0){//是坏键
if(!out[input[i]]){//没被输出过
printf("%c",input[i]);//输出此坏键
out[input[i]]=true;//此坏键已被输出过
}
i+=K;//每K个坏键在正确的字符串中只输出1次
}
else if(hashTable[input[i]]<0)//不是坏键
++i;//继续遍历下一字符
}
cout<<endl<<output;//输出正确的字符串
return 0;
}


• 擅长领域：
• 算法