zoukankan      html  css  js  c++  java
  • 【刷题-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() 函数的使用

  • 相关阅读:
    用面向对象的方法重写选项卡
    js 深入理解原型模式
    ECMAScript中的两种属性
    引用类型
    js 变量、作用域和内存问题
    html5 canvas画布尺寸与显示尺寸
    网页画板制作
    了解数组中的队列方法,DOM中节点的一些操作
    JavaScript中的数组对象遍历、读写、排序等操作
    this在方法赋值过程中无法保持(隐式丢失)
  • 原文地址:https://www.cnblogs.com/vinnson/p/10845081.html
Copyright © 2011-2022 走看看