【刷题】A1112 Stucked Keyboard-PAT甲级

版权声明:转载先点个赞嘛~~ https://blog.csdn.net/qq_39011762/article/details/88183119

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()函数的使用

猜你喜欢

转载自blog.csdn.net/qq_39011762/article/details/88183119
今日推荐