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

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章