zoukankan      html  css  js  c++  java
  • PAT A1112 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<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
    
     
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <map>
     5 #include <string>
     6 #include <vector>
     7 #include <queue>
     8 #include <set>
     9 using namespace std;
    10 int n,k,m;
    11 set<char> good,bad,pr;
    12 queue<char> q;
    13 int main(){
    14     scanf("%d",&n);
    15     string s;
    16     cin>>s;
    17     for(int i=0;i<s.length()-n+1;i++){
    18         char now=s[i];
    19         int flag=0;
    20         for(int j=1;j<n;j++){
    21             if(s[i]!=s[j+i]){
    22                 good.insert(now);
    23                 flag=1;
    24                 break;
    25             }
    26         }
    27         if(flag==1){
    28             if(bad.find(now)!=bad.end())bad.erase(bad.find(now));
    29             continue;
    30         }
    31         if(good.find(now)==good.end())bad.insert(now),i=i+n-1;
    32     }
    33     for(int i=0;i<s.length();i++){
    34         if(bad.find(s[i])!=bad.end() && pr.find(s[i])==pr.end())printf("%c",s[i]),pr.insert(s[i]);
    35     }
    36     printf("
    ");
    37     for(int i=0;i<s.length();i++){
    38         if(bad.find(s[i])==bad.end())printf("%c",s[i]);
    39         else{
    40             printf("%c",s[i]);
    41             i=i+n-1;
    42         }
    43     }
    44 }
    View Code

    注意点:看似简单,实际上还蛮复杂的。看了大佬的思路都是看重复的个数是不是n的倍数,再来判断。但总感觉都落下了一个考虑点,一开始认为是坏的,其实后面证明是好的,这个好像都没有考虑。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    将IIS中网站日志批量导入到mysql【python】
    Python网站日志分析
    python 获取文件版本号和修改时间
    python将IIS日志导入到SQL
    python2.7 MySQLdb模块在win32下安装
    《python核心编程》课后题第二版第十五章463页
    python批量文件重命名
    python随机生成彩票号码
    python获取IP归属地
    百度收录批量查询【python版】
  • 原文地址:https://www.cnblogs.com/tccbj/p/10438022.html
Copyright © 2011-2022 走看看