PAT甲级1112 Stucked Keyboard (20分) 测试点3,小细节多点

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

测试点3 是 caseee1__thiiis_iiisss_a_teeeeee这种,重复的字符在字符串最后

还有就是 对于任意一个字符ch,只要ch的连续出现的次数有一次没超过k,那么ch就不会是坏了的那个字符键盘。

然后感觉需要考虑的情况就没了,正常实现就好了

AC代码

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <math.h>
using namespace std;
int cmp(int a,int b)
{
    return a>b;
}

int main(void)
{
    int k;
    cin>>k;
    string str;
    cin>>str;
    map<char,int> mapp;
    int time=0;
    
    //查找哪些字符重复出现了k次
    for(int i=0; i<str.size(); i++)
    {
        if(time==0)//其实也就i=0的时候会出现time=0的情况
        {
            time++;
        }
        else if(str[i]==str[i-1])//当前的字符和前一个字符相等
        {
            time++;
        }
        else if(str[i]!=str[i-1])//字符不相等,就需要对前一个字符是否重复k次判断
        {
            //mapp[str[i-1]]=-1表示的是有字符有一次重复次数没超过k,
            //那么就肯定不会是坏的键盘嘛,就算这次重复次数超过k了,也不应该标记为坏的
            if(time%k==0&&mapp[str[i-1]]!=-1)
            {
                mapp[str[i-1]]=1;
            }
            else
            {
                mapp[str[i-1]]=-1;

            }
            time=1;
        }
    }
    //这里是对最后一个字符处理的
    //最后一个字符重复了k的倍数并且之前该字符重复出现次数没有小于K的
    if(time%k==0&&mapp[str[str.size()-1]]!=-1)
        mapp[str[str.size()-1]]=1;
    else if(time>0){
        mapp[str[str.size()-1]]=-1;
    }
    //输出坏的键盘字符
    map<char,int> sign;
    for(int i=0; i<str.size(); i++)
    {

        if(mapp[str[i]]==1)
        {
            if(sign[str[i]]==0){
                 cout<<str[i];
                 sign[str[i]]=1;
            }
        }
    }
    cout<<endl;
    
    
    for(int i=0; i<str.size(); i++)
    {
        if(mapp[str[i]]!=1)
            cout<<str[i];
        else
        {
            if(i==0)
                cout<<str[i];
            else if(str[i]!=str[i-1])
                cout<<str[i];
            //下面代码是对坏键盘重复了好几次的情况进行输出
            while(i+k<str.size()&&str[i+k]==str[i])
            {
                cout<<str[i];
                i+=k;
            }
            //因为for循环里面还有个++
            i+=k-1;
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值