(PAT 1112) Stucked Keyboard (模拟)

深碍√TFBOYSˉ_ 2022-03-17 08:22 279阅读 0赞

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

Sample Output:

  1. ei
  2. case1__this_isss_a_teest

解题思路:

要注意下面这些情况,比如 3 aabbaaa不存在坏键

3 aaabbaa也不存在坏键 ,用键码出现次数%K去判断

  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. using namespace std;
  7. int isBroken[128];
  8. int K;
  9. string inputs;
  10. vector<char> detectedAlpha;
  11. vector<char> sureBroken;
  12. map<char, int> appearTimes;
  13. int main() {
  14. fill(isBroken, isBroken + 128, -2);
  15. cin >> K;
  16. cin >> inputs;
  17. for (int i = 0; i < inputs.length(); ++i) {
  18. if (!appearTimes[inputs[i]]) { //用于二次判断
  19. appearTimes[inputs[i]] = 1;
  20. }
  21. else {
  22. appearTimes[inputs[i]]++;
  23. }
  24. char curChar = inputs[i];
  25. int appearTimes = 1;
  26. for (int j = 1; i + j < inputs.length(); j++) {
  27. if (inputs[i + j] != curChar) break;
  28. else {
  29. appearTimes++;
  30. }
  31. }
  32. if (appearTimes % K == 0) { //-2表示未知状态
  33. if (isBroken[(int)inputs[i]] == -2) {
  34. detectedAlpha.push_back(inputs[i]);
  35. isBroken[(int)inputs[i]] = -1; //-1表示损坏
  36. }
  37. }
  38. else {
  39. if (isBroken[(int)inputs[i]] == -2) {
  40. isBroken[(int)inputs[i]] = 1; //1表示正常
  41. }
  42. }
  43. }
  44. //二次判断
  45. for (auto itr = detectedAlpha.begin(); itr != detectedAlpha.end(); ++itr) {
  46. if (appearTimes[*itr] % K == 0) { //依然正常
  47. sureBroken.push_back(*itr);
  48. }
  49. else {
  50. isBroken[(int)*itr] = 1;
  51. }
  52. }
  53. //按顺序输出
  54. for (auto x : sureBroken) {
  55. printf("%c", x);
  56. }
  57. cout << endl;
  58. //输出字符
  59. int ind = 0;
  60. for (; ind < inputs.length();) {
  61. printf("%c", inputs[ind]);
  62. if (isBroken[(int)inputs[ind]] == -1) {
  63. ind += K;
  64. }
  65. else {
  66. ind++;
  67. }
  68. }
  69. system("PAUSE");
  70. return 0;
  71. }

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CCF I’m stuck!

    一、试题 问题描述   给定一个R行C列的地图,地图的每一个方格可能是’\’, ‘+’, ‘-‘, ‘|’, ‘.’, ‘S’, ‘T’七个字符中的一个,分别表示如下