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 

思路:判断键是否是坏的,要看连续相同字符的个数是否能被k整除,因为如果是坏的,按一下就会重复k次。

程序:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  string s;
  cin>>s;
  int arr[37] = {0};//先0-9,-,最后11-36位a-z,
  int count = 1;
  for(int i = 1; i < s.size(); i++)
  {
    if(s[i] != s[i-1] && count % n != 0)
    {
      count = 1;
      if(s[i-1] == '_')
        arr[10] = 1;
      else if(s[i-1] >= '0' && s[i-1] <= '9')
        arr[s[i-1]-'0'] = 1;
      else
        arr[s[i-1]-'a'+11] = 1;
    }
    else if(s[i] != s[i-1])
      count = 1;
    else if(s[i] == s[i-1])
      count++;
  }
  if(count % n != 0)
  {
    char ch = s[s.size()-1];
    if(ch == '_')
      arr[10] = 1;
    else if(ch >= '0' && ch <= '9')
      arr[ch-'0'] = 1;
    else
      arr[ch-'a'+11] = 1;
  }
  string s1;
  for(int i = 0; i < s.size(); i++)
  {
    char ch = s[i];
    if(ch == '_')
    {
      if(!arr[10])
      {
        s.erase(i+1,n-1);
        if(s1.find('_') == -1)
          s1 += '_';
      }
    }
    else if(ch >= '0' && ch <= '9')
    {
      if(!arr[ch-'0'])
      {
        s.erase(i+1,n-1);
        if(s1.find(ch) == -1)
          s1 += ch;
      }
    }
    else
    {
      if(!arr[ch-'a'+11])
      {
        s.erase(i+1,n-1);
        if(s1.find(ch) == -1)
          s1 += ch;
      }
    }
  }
  cout<<s1<<endl;
  cout<<s<<endl;
  return 0;
}

猜你喜欢

转载自blog.csdn.net/hickey_chen/article/details/81002778
今日推荐