solutino Of Pat 1112. Stucked Keyboard (20)

48 篇文章 0 订阅

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


结题思路:
题意要求我们找到那些卡住的键位。
要求1:重复的次数人为指定为k(k大于0小于等于100);
要求2:输入的字符串当中的字符属于{a-z}, {0-9} 和 “_”;
要求3:对于在字符串识别过程中之前/之后认为的卡键,如果在后续遍历中/当前发现他的连续重复小于k,此时我们认为当前键位正常 。

程序步骤:
第一步、暂存字符串和k值;
第二步、直接进行重复键位的判断;
第二步1、遇新字符,保存,将pre字符的stucked置false,初始化count=1;
第二步2、遇老字符,判断与pre的关系(不相等),将pre字符的stucked置false,初始化count=1;
第二步3、遇老字符,判断与pre的关系(相等),++count,初始化pre和count;
第三步、输出。

具体程序(AC)如下:

#include<iostream>
#include<string>
#include<vector>
#define BMax 200
using namespace std;
int main()
{
    vector<bool> exist;//存在标记符
    vector<char> input;//存放输入的字符种类
    vector<bool> stucked;//卡位的标记符
    exist.resize(BMax+5,false);
    input.clear();
    int k;
    string s;
    cin>>k>>s;
    stucked.resize(BMax+5,false);
    int pre=BMax;//暂存当前字符的前一个字符
    int count=0;
    //初始化,暂存字符串和k值。
    for(int i=0;i<s.length();++i)
    {
        if(!exist[s[i]])
        {
            exist[s[i]]=true;
            input.push_back(s[i]);
            stucked[pre]=false;//pre对应的字符在最后的条件判断中已经做出处理,故这里直接进行了false的赋值
            pre=s[i];
            count=1;
        }
        else
        {
            if(s[i]!=pre)
            {
                stucked[pre]=false;//pre对应的字符在最后的条件判断中已经做出处理,不需要再进行重复次数的判断。
                pre=s[i];
                count=1;
            }
            else
            {
                ++count;
                if(count==k)//重复次数等于指定的k值时,认为是卡位,进行标记,同时将pre重新初始化
                {
                    if(!stucked[pre])//???经测此处判断与否都能ac
                    stucked[pre]=true;
                    pre=BMax;
                    count=0;
                }
            }
        }
    }
    stucked[pre]=false;
    for(int i=0;i<input.size();++i)
        if(stucked[input[i]])
            cout<<input[i];
    cout<<endl;
    for(int i=0;i<s.length();++i)
    {
        cout<<s[i];
        if(stucked[s[i]])
            i+=k-1;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值