pat甲级1112. Stucked Keyboard (20)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/richenyunqi/article/details/80156289

欢迎访问我的pat甲级题解目录哦https://blog.csdn.net/richenyunqi/article/details/79958195

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

算法设计:

这道题处理起来比较麻烦,定义一个长度为128的int数组hashTable,下标表示字符,元素为1表示该字符是坏键,为-1表示该字符不是坏键,元素为0表示该字符还没有遇到过。遍历输入的字符串input,统计连续出现的重复字符的个数是否是K的整数倍,若不是表示该字符必然不是坏键;如果在输入的字符串input中某字符连续出现的重复个数都是K的整数倍,那么该字符是坏键。注意,是坏键的条件比较苛刻,只要某字符有一次重复出现的个数不是K的整数倍,那么此键就不是坏键。

再次遍历字符串Input,将hashTable对应元素大于0的坏键进行输出,注意同一个字符只能输出1次,且需按在字符串input中出现的顺序进行输出。然后输出正确的字符串,每K个坏键输出一次,每1个良好的键都输出1次即可。

注意点:

  1. 是坏键的条件比较苛刻,只要某字符有一次重复出现的个数不是K的整数倍,那么此键就不是坏键。
  2. 输出坏键时同一个坏键只能输出1次,且需按在字符串input中出现的顺序进行输出。

c++代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string input,output="";
    int K;
    cin>>K>>input;
    int hashTable[128]={0};//表示是不是坏键
    for(int i=0;i<input.size();){
        int j=i+1;
        while(j<input.size()&&input[j]==input[i])//找到i位置后第一个不等于input[i]的字符位置
            ++j;
        if(hashTable[input[i]]>=0)//当前的input[i]不是坏键
            hashTable[input[i]]=(j-i)%K==0?1:-1;//通过连续出现的input[i]的个数是否是K的整数倍给hashTable赋值1、-1
        i=j;
    }
    bool out[128]={false};//表示字符是否已被输出过
    for(int i=0;i<input.size();){
        output+=input[i];//将当前字符加入正确的字符串中
        if(hashTable[input[i]]>0){//是坏键
            if(!out[input[i]]){//没被输出过
                printf("%c",input[i]);//输出此坏键
                out[input[i]]=true;//此坏键已被输出过
            }
            i+=K;//每K个坏键在正确的字符串中只输出1次
        }
        else if(hashTable[input[i]]<0)//不是坏键
            ++i;//继续遍历下一字符
    }
    cout<<endl<<output;//输出正确的字符串
    return 0;
}

阅读更多

扫码向博主提问

日沉云起

用最短的代码,写最快的算法
  • 擅长领域:
  • 算法
去开通我的Chat快问
换一批

没有更多推荐了,返回首页