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
解题思路:字符采用常用的ACSII值进行映射。本题与通常的做法不太一样的地方是,我们标记没有坏的键,而不是标记坏键,因为,只要有一处的键完好,就说明该按键没有问题。
#include <iostream>
using namespace std;
bool well[256],visit[256];
int main(){
int k, len;
string s,ans;
cin>>k>>s;
len = s.length();
for(int i = 0; i < len;){
int j = i;
while(j < len && s[j] == s[i])
j++;
if((j - i) % k != 0)
well[s[i]] = true;
else
ans += s[i];
i = j;
}
k--;
for(int i = 0; i < ans.length(); i++)
if(well[ans[i]] == false && visit[ans[i]] == false){
printf("%c",ans[i]);
visit[ans[i]] = true;
}
printf("\n");
for(int i = 0; i < len; i++){
printf("%c",s[i]);
if(well[s[i]] == false)
i += k;
}
}