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的倍数,再来判断。但总感觉都落下了一个考虑点,一开始认为是坏的,其实后面证明是好的,这个好像都没有考虑。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    Python 杨辉三角形
    Python 输出由星号*组成的菱形图案
    Python 计算组合数C(n,i),即从n个元素中任选i个,有多少种选法
    Python 快速判断一个数是不是素数
    判断今天是今年的第几天
    Pyhon 输入若干个成绩,求所有成绩的平均分。每输入一个成绩后询问是 否继续输入下一个成绩,回答“yes”就继续输入下一个成绩,回答“no” 就停止输入成绩
    KMP算法
    递归实现求解幂集问题
    Python 用大量小矩形来画曲线
    Python 所谓的艺术操作2(带颜色)
  • 原文地址:https://www.cnblogs.com/tccbj/p/10438022.html
Copyright © 2011-2022 走看看