zoukankan      html  css  js  c++  java
  • 1112 Stucked Keyboard (20 分)(map)【回顾】

    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

    生词

    英文 解释
    stucked 粘滞的

    题目大意:

    键盘某些键卡住了,按一次重复k次,要求找出可能的键,并且输出正确的字符串顺序。可能的键要求按照被发现的顺序输出~

    分析:

    考察STL的应用~
    map<char, bool>存储出现的键是否坏,set存储输出可能坏的键的时候,当前字符是否已经被输出过,输出过的键放在set里面.

    寻找坏键:

    遍历字符串的每个字符的时候,与pre(字符串当前字符s[i]的前一个字符)相比较,如果相等就继续计数cnt++,如果不相等,令cnt = 1表示当前字符出现了一次~~如果cnt % k等于0 则令s[i]可能是坏键,置map对应的字符的bool值为true~

    输出坏键:

    由于需要根据坏键发现的顺序输出,所以遍历整个字符串的方式输出,并且确保不会重复输出(用set集合确保,输出过了的放在set里面)

    输出整个正确的字符串:

    如果当前s[i]是坏键,在输出一次后,令 i = i + k – 1,再输出,保证坏键出现k次只输出一次~

    tips:

    如果出现了先不是坏建后又判断是坏键的情况,这种情况会出现错误,因为前面已经认为它不是坏键了,说明它一定不是坏建,所以要加一个sureNoBroken,把确定不是坏键的键标记出来,在map都设置完成后把确定不是坏键的m标记为false。虽然测试用例里面没有考虑到这种情况,但是如果输入3 aabbaaa,应该输出没有坏键~

    原文链接:https://blog.csdn.net/liuchuo/article/details/52124990

    题解

    逻辑思维还不太行。。

    #include <bits/stdc++.h>
    
    using namespace std;
    map<char,bool> m;
    bool sureNoBroken[256];
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int k,cnt=1;
        string s;
        set<char> st;
        cin>>k;
        cin>>s;
        char pre='#';
        s+="#";
        for(int i=0;i<s.size();i++){
            if(pre==s[i]) cnt++;
            else{
                if(cnt%k!=0){
                    //这个地方的bug又找了大半天,,,写成了sureNoBroken[s[i]]=true;
                    sureNoBroken[pre]=true;
                }
                cnt=1;
            }
            if(i!=s.size()-1) m[s[i]]=(cnt%k==0);
            pre=s[i];
        }
        for(int i=0;i<s.size()-1;i++){
            if(sureNoBroken[s[i]]) m[s[i]]=false;
        }
        for(int i=0;i<s.size()-1;i++){
            if(m[s[i]]&&st.find(s[i])==st.end()){
                cout<<s[i];
                st.insert(s[i]);
            }
        }
        cout<<endl;
        for(int i=0;i<s.size()-1;i++){
            cout<<s[i];
            if(m[s[i]]){
                i=i+k-1;
            }
        }
        return 0;
    }
    

    本文来自博客园,作者:勇往直前的力量,转载请注明原文链接:https://www.cnblogs.com/moonlight1999/p/15766024.html

  • 相关阅读:
    .NET Core 使用Dapper 操作MySQL
    .NET Core HtmlAgilityPack HTML解析利器
    ASP.NET Core 开发-缓存(Caching)
    .NET Core 调用WCF 服务
    ASP.NET Core 开发-Logging 使用NLog 写日志文件
    Qt动态生成界面并通过拉姆达获取其返回值
    Qt启动C++线程并在线程中修改界面
    Vector求最大值最小值
    C/C++取消结构体字节对齐
    Matlab定时器
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15766024.html
Copyright © 2011-2022 走看看