PAT-A 真题 – 1112 Stucked Keyboard

发布于 / PAT-甲级 / 0 条评论

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

题目大意:一个键盘坏掉了一些按键,坏掉的按键按一下相当于按k下,给你一串字符串,判断键盘的哪个键子是坏掉的。

注意:如果这个键子正常过,则不能算坏键。例如k=3,字符串aaabccca中,a是正常的,c是坏的。

注意:如果重复次数不是k的倍数,则它一定不是坏键。例如k=3,abcccca中c不是坏的。

按照发现的先后顺序,输出坏掉的键子,并输出正确的文字。

注意:例如k=3,则aaaaaabbc正确的文字应该是aabbc。

这道题模拟一下即可

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
#include <algorithm>
#define MAXN 1010
using namespace std;

int main(){
  char str[MAXN], out[MAXN];
  int n;
  scanf("%d %s", &n, str);
  vector<char> Good, Bad;
  int Repeat = 1;
  char last = 0;
  //找到键盘上好用的键子 
  for(int i = 0; i < strlen(str)+1; i++){
    if(str[i] != last){    //如果当前字符串与上一个不同 
      //判断Repeat是否整除n,没整除说明键子是好的 
      if(Repeat % n && last) Good.push_back(last);
      last = str[i], Repeat = 1;
    }else Repeat++;
  }
  //接着遍历输出,遇到好用的存入字符串,遇到不好的在容器中检查去重输出 
  int it = 0;
  for(int i = 0; i < strlen(str); ){
    if(find(Good.begin(), Good.end(), str[i]) != Good.end()){
      out[it++] = str[i++];
    }else{
      if(find(Bad.begin(), Bad.end(), str[i]) == Bad.end()){
        Bad.push_back(str[i]);
        cout << str[i];
      }
      out[it++] = str[i];
      i += n;
    }
  }
  out[it] = 0;
  cout << endl << out;
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题 – 1112 Stucked Keyboard
目前还没有评论,快来抢沙发吧~