A1112 Stucked Keyboard (20 point(s))

坏键问题

1. 原文

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
2
3
caseee1__thiiis_iiisss_a_teeeeeest

Sample output:

1
2
ei
case1__this_isss_a_teest

2. 解析

坏键,循环对比,当字符不相等时判断个数,但坏键只能是可能

1
2
3
4
5
6
7
8
9
10
11
12
  if (str[i]==str[i+1])
{
cnt++;
}else{
if (cnt%k==0)
{
bad[str[i]]=true;
}else{
good[str[i]]=true;
}
cnt=1;
}

当它既是坏键又是好键,当作好键

1
2
3
4
5
6
7
for (int i = 0; i < str.length(); ++i)
{
if (good[str[i]])
{
bad[str[i]]=false;
}
}

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<char,bool> good,bad;
int main()
{
int k;
cin>>k;
string str;
cin>>str;
int cnt=1;
for (int i = 0; i < str.length(); ++i)
{
if (str[i]==str[i+1])
{
cnt++;
}else{
if (cnt%k==0)
{
bad[str[i]]=true;
}else{
good[str[i]]=true;
}
cnt=1;
}
}
for (int i = 0; i < str.length(); ++i)
{
if (good[str[i]])
{
bad[str[i]]=false;
}
}
string broken;
for (int i = 0; i < str.length(); ++i)
{
if (bad[str[i]]&&broken.find(str[i])==string::npos)
{
broken+=str[i];
}
}
string ans;
for (int i = 0; i < str.length(); ++i)
{
if (bad[str[i]]==false)
{
ans+=str[i];
}else{
ans+=str[i];
i+=k-1;
}
}
cout<<broken<<"\n"<<ans<<endl;

return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1112/
  • 发布时间: 2019-09-03 12:30
  • 更新时间: 2021-10-29 14:08
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!