zoukankan      html  css  js  c++  java
  • PAT 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个字符只需要输出1个;
    思路:vis记录重复的字符是否已经出现, num表示字符的状态,num[s[i]]=-1;表示s[i]不是被卡住的按键, num[s[i]]=0; 表示按键的值第一次出现, num[s[i]]=1 表示s[i]是被卡住的按键

    注意点:可能有这样的情况k=3; cabbbbwe; 这里b出现了4次,但它不是被卡住的按键,要连续出现的次数是k的整数倍才是卡住的按键
     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 int main(){
     5   int k, i;
     6   string s;
     7   cin>>k>>s;
     8   vector<int> num(128, 0), vis(128, 0);
     9   vector<char> ans;
    10   int cnt=1;
    11   for(i=0; i<s.size()-1; ){
    12       while(s[i]==s[i+1]){
    13         cnt++;
    14         i++;
    15       }
    16       if(cnt>=k && cnt%k==0 && num[s[i]]!=-1){
    17           if(!vis[s[i]]) ans.push_back(s[i]);
    18           num[s[i]]=1;
    19           vis[s[i]] = 1;
    20       }
    21       else num[s[i]] = -1;
    22       cnt=1;
    23       i++;
    24   }
    25   for(i=0; i<ans.size(); i++) if(num[ans[i]]!=-1)cout<<ans[i];
    26   cout<<endl;
    27   for(i=0; i<s.size();){
    28      if(num[s[i]]!=1){ cout<<s[i]; i++;}
    29     else{
    30         char t=s[i];
    31         cout<<s[i];
    32         i += k;
    33     }
    34   }
    35   return 0;
    36 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    正则表达式验证手机号
    windows redis 安装启动及设置密码
    c# MVC利用AuthorizeAttribute验证用户是否登录
    判断页面是否加载完成
    关于List的ConcurrentModificationException
    intellij idea 12 编码不可映射字符
    这年头,竞争压力这么大,为什么我啥都想学。
    关于学习
    盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
    单点登录原理与简单实现
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9232980.html
Copyright © 2011-2022 走看看