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
emmm这题怎么说才20分不过自己wa了好久
题目意思就是检查下哪些键是错误的
嗯错误的标准就是连续输入的数的个数小于n 或者是连续输入的数的个数不是n的倍数就是对的 第二个测试点就是wa在连续输入的个数不是n的倍数这里 我改了好久还是没有改出来
代码:
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
char str[1005],ch = '*';
scanf("%s",str);
int len = strlen(str),cnt = 0;
map<char,int>mp;
for(int i = 0 ; i <= len ; i++)
{
if(str[i] != ch)
{
if(cnt < n || cnt % n != 0)
mp[ch] = 1;
cnt = 1;
ch = str[i];
}else
cnt ++;
}//ok 现在知道哪些是没有坏掉的键盘了
int flag = 1;
for(int i = 0 ; i < len ; i++)
{
if(mp[str[i]] == 0)
{
printf("%c",str[i]);
mp[str[i]]=-1;
flag = 0 ;
}
}
printf("\n");
for(int i = 0 ; i < len ; i++)
{
if(mp[str[i]] == 1)
printf("%c",str[i]);
else
{
cnt = 1;
ch = str[i];
while(str[++i]==ch)
++cnt;
--i;
for(int j = 1; j <= cnt / n;j++)
printf("%c",ch);
}
}
printf("\n");
return 0;
}