zoukankan      html  css  js  c++  java
  • PAT甲级——A1112 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) 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


    注意点

    1. 是坏键的条件比较苛刻,只要某字符有一次重复出现的个数不是K的整数倍,那么此键就不是坏键。
    2. 输出坏键时同一个坏键只能输出1次,且需按在字符串input中出现的顺序进行输出。
     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main()
     5 {
     6     int n;
     7     string str, res = "", bad = "", temp = "";
     8     int key[128] = { 0 };
     9     cin >> n;
    10     getchar();
    11     getline(cin, str);
    12     for (int i = 0; i < str.length();)
    13     {
    14         int j = i + 1;
    15         while (j < str.length() && str[j] == str[i])//重复字符
    16             ++j;
    17         if (key[str[i]] >= 0)//不是坏键
    18             key[str[i]] = (j - i) % n == 0 ? 1 : -1;//重复n次是坏键
    19         i = j;
    20     }        
    21     bool out[128] = { false };//表示是否输出过
    22     for (int i = 0; i < str.length();)
    23     {
    24         res += str[i];
    25         if (key[str[i]] == 1)//为坏键
    26         {
    27             if (out[str[i]] == false)//还为输出过
    28                 bad += str[i];
    29             out[str[i]] = true;//输出过
    30             i += n;//跳过重复键
    31         }
    32         else
    33             ++i;//继续遍历
    34     }
    35     cout << bad << endl << res << endl;
    36     return 0;
    37 }
  • 相关阅读:
    Tuxedo 介绍
    winform如何不卡界面
    银行基金代销系统调研
    如何在wcf中用net tcp协议进行通讯
    20190710用控制台启动一个wcf服务
    wcf必知必会以及与Webapi的区别
    2019年7月第一周总结-RabbitMQ总结
    RabbitMQ入门学习系列(七) 远程调用RPC
    RabbitMQ入门学习系列(六) Exchange的Topic类型
    RabbitMQ入门学习系列(五) Exchange的Direct类型
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11455325.html
Copyright © 2011-2022 走看看