zoukankan      html  css  js  c++  java
  • A1112. Stucked Keyboard

    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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<string.h>
     5 using namespace std;
     6 int hashTB[128];
     7 int main(){
     8     char str[1001], prt[1001] = {0};
     9     int N, K;
    10     fill(hashTB, hashTB + 128, 1);
    11     scanf("%d", &K);
    12     scanf("%s", str);
    13     N = strlen(str);
    14     for(int i = 0; i < N; i++){
    15         int tag = 1;
    16         if(i + K > N){
    17             hashTB[str[i]] = 0;
    18             continue;
    19         }
    20         for(int j = i; j < i + K; j++){
    21             if(str[j] != str[i]){
    22                 hashTB[str[i]] = 0;
    23                 tag = 0;
    24                 break;
    25             }
    26         }
    27         if(tag == 1){
    28             i += (K - 1);
    29         }
    30     }
    31     int flag = 0;
    32     for(int i = 0; i < N; i++){
    33         if(hashTB[str[i]] == 1 && prt[str[i]] == 0){
    34             printf("%c", str[i]);
    35             prt[str[i]] = 1;
    36             flag = 1;
    37         }
    38     }
    39     if(flag == 1)
    40         printf("
    ");
    41     for(int i = 0; i < N; i++){
    42         if(hashTB[str[i]] == 0){
    43             printf("%c", str[i]);
    44         }else{
    45             printf("%c", str[i]);
    46             i += K - 1;
    47         }
    48     }
    49     cin >> N;
    50     return 0;
    51 }
    View Code

    总结:

    1、题意:给出一个字符串和粘连次数K,检查这个键是否是坏键。

    2、使用哈希表。初始时假设所有键都是坏的,然后遍历,发现a没有粘连,则说明a是好的。 注意不能假设所有键是好的,然后发现坏件再设置为坏。因为aaaba,则会错误将a识别为坏件。

    3、注意最后K个字符的判断。

  • 相关阅读:
    349、两个数组的交集 | JS集合
    JS集合Set
    JS里的队列和链表
    使用链表指针获取JSON的节点值
    141、环形链表 | JS-链表
    83、删除排序链表中的重复元素 | JS-链表
    2、两数相加 | JS-链表
    事件循环与任务队列
    933、最近的请求次数 | JS-队列
    栈JS实现
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8574855.html
Copyright © 2011-2022 走看看