• 【刷题-PAT】A1112 Stucked Keyboard (20 分)


    1112 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<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
    

    分析:找出没有坏的按键,寻找连续的相等的字符,当其长度不是k的倍数时,按键一定是好的,置 well[ ] 数组为 true,然后遍历输入的字符串,如果是好的就输出,坏的就记录下来

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<unordered_map>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int main(){
        #ifdef ONLINE_JUDGE
        #else
        freopen("input.txt", "r", stdin);
        #endif // ONLINE_JUDGE
        int k;
        scanf("%d", &k);
        getchar();
        string str;
        getline(cin, str);
        bool well[260] = {false};
        int i = 0, j = 0;
        while(i < str.size()){
            while(j < str.size() && str[i] == str[j])j++;
            if((j - i) % k != 0)well[(int)str[i]] = true;
            i = j;
        }
        string ans1, ans2;
        i = 0;
        while(i < str.size()){
            ans1 += str[i];
            if(well[str[i]] == false){
                if(ans2.find(str[i]) == string::npos)ans2 += str[i];
                i += k;
            }else i++;
        }
        cout<<ans2<<endl;
        cout<<ans1<<endl;
        return 0;
    }
    

    注意:string中的find() 函数的使用

  • 相关阅读:
    微信小程序之授权 wx.authorize
    微信小程序之可滚动视图容器组件 scroll-view
    纯 CSS 利用 label + input 实现选项卡
    Nuxt.js + koa2 入门
    koa2 入门(1)koa-generator 脚手架和 mongoose 使用
    vue 自定义指令
    时运赋
    WEBGL 2D游戏引擎研发系列 第一章 <新的开始>
    EasyUI特殊情况下的BUG整理
    数字时钟DigClock
  • 原文地址:https://www.cnblogs.com/vinnson/p/10845081.html
Copyright © 2020-2023  润新知