PAT_A 1112. Stucked Keyboard (20)

来源:互联网 发布:淘宝售后期限是多久 编辑:程序博客网 时间:2024/04/28 00:50

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:3caseee1__thiiis_iiisss_a_teeeeeestSample Output:eicase1__this_isss_a_teest
  • 简述
    该题目确定卡住的字符,即出现的时候都是k的倍数即可。
  • 代码
#include<iostream>#include<string>#include<map>using namespace std;//出现的字母如果全部是连续三次,则卡住了//否则不能判断int main(){    map<char,int>r;    int n;    cin>>n;    string tmp;    cin>>tmp;    for(int i=0;i<tmp.length();i++)    {        if(r.find(tmp.at(i))==r.end())            r[tmp.at(i)]=0;    }    for(int i=0;i<tmp.length();i++)    {        int count=0;        char c=tmp.at(i);        if(r[c]==-1)          continue;        for(int j=i;j<tmp.length();j++)        {            if(c==tmp.at(j))              count++;            else              break;        }        if(count%n==0)        {          r[c]=1;          i+=(count-1);        }        else        {          r[c]=-1;        }    }    string o1;    string o2;    for(int i=0;i<tmp.length();i++)    {        char c=tmp.at(i);        if(r.at(c)==1)        {            if(o1.find(c)==string::npos)                o1+=c;            i+=(n-1);        }        o2+=c;    }    cout<<o1<<endl        <<o2<<endl;    return 0;}
0 0
原创粉丝点击