zoukankan      html  css  js  c++  java
  • PAT1112:Stucked Keyboard

    1112. Stucked Keyboard (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    思路

    1.用map模拟一个字典dic记录可能卡住的坏键,bool数组用来确定是否是坏键。
    2.连续序列中出现次数不小于k的字母可能是坏键,进一步确定如下:
    可能出现的情况:
    1) 字母key第一次出现时如果连续次数不下于k次可能是坏键,如果接下来key连续出现仍然不小于k次,那么它一定是坏键,反之不是。
    2) 如果字母key第一次出现时连续次数小于k次,那么它一定不是坏键,之后不管key连续出现多少次(哪怕超过k)我们也知道它不是坏键,因为那属于正常输出而不是卡键。
    3.所以,如果字符串中某个字符只出现了一次超过k次的情况,那么也属于正常范围。

    代码
    #include<iostream>
    #include<vector>
    #include<map>
    #include<set>
    using namespace std;
    vector<bool> NotBroken(128,false);//处理已经确认不是坏键但之后出现重复次数超过k的情况
    int main()
    {
        int k,cnt = 0;
        string s;
        while(cin >> k >> s)
        {
            map<char,bool> dic; //是否是坏键
            set<char> isPrinted;
            char prev = '*'; //当前i位置字符的前驱字符
            s += '*'; //处理到最后一位都是stuck的情况
            for(int i = 0; i < s.size();i++)
            {
                if(s[i] == prev)
                    cnt++;
                else
                {
                    if(cnt % k != 0)
                        NotBroken[s[i]] == true;
                    cnt = 1;
                }
                if( i != s.size() - 1)
                    dic[s[i]] = (cnt % k == 0);
                prev = s[i];
            }
            //将已经确认不是坏键但之后出现超过k次的情况排除
            for(int i = 0;i < s.size() - 1;i++)
            {
                if(NotBroken[s[i]])
                    dic[s[i]] = false;
            }
    
            //打印坏键,因要按发现的顺序输出,所以不能直接遍历map
            for(int i = 0;i < s.size() - 1;i++)
            {
                if(dic[s[i]] && isPrinted.find(s[i]) == isPrinted.end())
                {
                    cout << s[i];
                    isPrinted.insert(s[i]);
                }
            }
            cout << endl;
            //输出处理后的字符串
            for(int i = 0;i < s.size() - 1;i++)
            {
                cout << s[i];
                if(dic[s[i]])
                   i += k - 1;
            }
    
        }
    }
    

      

  • 相关阅读:
    简体中文和繁体中文的转换
    EasyPoi 快速Office 开发
    玩转SpringBoot之定时任务详解
    Spring Boot中使用Swagger2构建强大的RESTful API文档
    一篇文章带你搞懂 SpringBoot与Swagger整合
    JAVA团队开发手册
    CentOS(6、7)修改主机名(hostname)
    SpringBoot多环境部署,在启动时动态设置相应的配置文件
    MyBatis逆向工程代码的生成以及使用详解(持续更新)
    MySQL写入插入数据优化配置
  • 原文地址:https://www.cnblogs.com/0kk470/p/7884063.html
Copyright © 2011-2022 走看看