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 kk 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 kk times whenever it is pressed. For example, when k=3k=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)k(1\lt k\le100) 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

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

int main(){
    int k;
    string str;
    int record[128];
    fill(record, record+128, 100);
    scanf("%d", &k);
    cin>>str;
    int i = 0, j = 1;
    while(j < str.length()){
        if(str[i] != str[j]){
            record[str[i]] = min(record[str[i]], j - i);
            i = j;
            j++;
        }
        else{
            if(j-i+1 == k){
                record[str[i]] = min(record[str[i]], k);
                i = j + 1;
                j = i + 1;
            }
            else
                j++;
        }
    }
    record[str[i]] = min(record[str[i]], j - i);
//    for(int i=0; i<str.length(); i++)
//        printf("%d ", record[str[i]]);
    int temp[128];
    copy(record, record+128, temp);
    for(int i=0; i<str.length(); i++){
        if(temp[str[i]] == k){
            printf("%c", str[i]);
            temp[str[i]] = 0;
        }
    }
    printf("\n");
    i = 0;
    while(i < str.length()){
        printf("%c", str[i]);
        if(record[str[i]] == k)
            i += k;
        else
            i++;
    }
    printf("\n");
    return 0;
}
发布了157 篇原创文章 · 获赞 72 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览