1112 Stucked Keyboard
题目
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:
1 | 3 |
Sample Output:
1 | ei |
题意
在一个坏掉的键盘上有一些按键是卡住的,所以当我们输入句子的时候,总有一些按键会固定重复出现K次,每按一下这个坏了的按键,字符就会重复出现K次,现在给定一个重复次数K,和给一个结果字符串,尝试找出粘键的按键和原始的字符串,每一个结果字符串保证至少有一个坏掉的按键.
注意,上面的例子中,e和i是坏掉的按键,因为每次按一下e和i,他们都会重复3次,但是s却不是,因为它并没有每按一次s就重复3次.
思路
这道题需要记录每个按键是否坏掉,也就是说我们要记录字符串里每个字符的具体情况,所以可以使用哈希表.
哈希表内每个字符对应的位置的值,代表着这个按键的情况:
如果还没判断,按键默认值就是0.
如果判断出这个按键没有坏,就将值设置为-1.
判断按键已经卡住了,就把按键设置为1.
要如何判断按键是否卡住,可以设置一个变量为nowKey,表示现在正在判断的按键字符;再设置一个nowKeyCount变量表示这个正在判断的字符已经连续出现的次数,如果nowKeyCount%K==0表示这个nowKey重复出现了k的倍数次,是坏键;如果不等于0,说明nowKey出现的次数不是k的倍数,这个键不是坏键.
代码
1 | public static void Main() |