1112 Stucked Keyboard

时间:2020-04-25
本文章向大家介绍1112 Stucked Keyboard,主要包括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) 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

题意:

  给出电脑上屏幕的输出结果,问那几个键是坏键(在输出的结果中总是重复出现n次)

思路:

  先遍历一遍字符串找到所有没有坏的建将其存放入well集合中,再遍历一边字符串所有没有出现在well集合中的键都是坏的建,将其存入broken集合中用来判断是不是第一次出现。如果是第一次出现则将其加入到坏建字符串中。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int n;
 7     string test;
 8     cin >> n >> test;
 9     int len = test.length();
10     set<int> broken, well;
11     string possible = "";
12     for (int i = 0; i < len; ++i) {
13         bool isBroken = true;
14         for (int j = i + 1; j < i + n && j < len; ++j) {
15             if (test[j] != test[j - 1]) {
16                 well.insert(test[i]);
17                 isBroken = false;
18                 break;
19             }
20         }
21         if (isBroken) i += n - 1;
22     }
23     for (int i = 0; i < len; ++i) {
24         if (well.find(test[i]) == well.end()) {
25             if (broken.find(test[i]) == broken.end()) possible += test[i];
26             broken.insert(test[i]);
27             i += n - 1;
28         }
29     }
30 
31     cout << possible << endl;
32     string ans = "";
33     for (int i = 0; i < len; ++i) {
34         if (broken.find(test[i]) == broken.end())
35             ans += test[i];
36         else {
37             ans += test[i];
38             i += n - 1;
39         }
40     }
41     cout << ans << endl;
42     return 0;
43 }

有一组数据被卡到了,还没找到原因。

原文地址:https://www.cnblogs.com/ruruozhenhao/p/12774484.html