PAT 甲级 1112 Stucked Keyboard

https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960

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) 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

代码(From  FH):

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;

int k;
string s;

map<char, int> mp, out;

struct X {
    char sign;
    int cnt;
}q[maxn];
int sz;

int main() {
    cin >> k >> s;

    int len = s.length();

    char Sign = s[0];
    int Cnt = 1;

    for(int i = 1; i < len; i ++) {
        if(s[i] == Sign) {
            Cnt ++;
        } else {
            q[sz].sign = Sign;
            q[sz].cnt = Cnt;
            sz ++;

            Sign = s[i];
            Cnt = 1;
        }
    }

    q[sz].sign = Sign;
    q[sz].cnt = Cnt;
    sz ++;

    /*for(int i = 0; i < sz; i ++) {
        cout << q[i].sign << " " << q[i].cnt << endl;
    }*/

    for(int i = 0; i < sz; i ++) {
        if(q[i].cnt % k != 0) mp[q[i].sign] = 1;
    }

    for(int i = 0; i < sz; i ++) {
        if(mp[q[i].sign] == 0 && out[q[i].sign] == 0) {
            cout << q[i].sign;
            out[q[i].sign] = 1;
        }
    }
    cout << endl;

    for(int i = 0; i < sz; i ++) {
        int num = q[i].cnt;
        if(mp[q[i].sign] == 0) num = num / k;
        while(num --) cout << q[i].sign;
    }
    cout << endl;

    return 0;
}

之前自己写了一个 18 的因为没有考虑 sss_s 的情况 这样的情况 s 是不卡的键 所以用 q 记下每一段重复的字符以及出现次数然后再输出答案 还是 FH 厉害哇 蹭蹭

(这个是我 wa 掉的代码)

20 分的题目能被我写成这个样子真滴想抽自己了 我是猪吧

#include <bits/stdc++.h>
using namespace std;

int N;
string s;
map<char, int> mp;
map<char, int> vis;

int main() {
    scanf("%d", &N);
    cin >> s;
    int len = s.length();
    mp.clear(); vis.clear();

    string ans = "";
    string out = "";
    for(int i = 0; i < len;) {
        char temp = s[i];
        int cnt = 0;
        if(mp[temp]) {
            out += temp;
            i ++;
            continue;
        }
        for(int j = i; j < len; j ++) {
            if(s[j] == temp) cnt ++;
            else break;
        }

        if(cnt % N == 0) {
            if(vis[temp] == 0) {
                ans += temp;
                vis[temp] = 1;
            }
            for(int j = 0; j < cnt / N; j ++)
                out += s[i];

            i += cnt;
        } else {
            mp[s[i]] = 1;
            out += s[i];
            i ++;
        }
    }

    cout << ans << endl << out << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/zlrrrr/p/10301227.html

时间: 2024-10-04 01:11:41

PAT 甲级 1112 Stucked Keyboard的相关文章

PAT 1112 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

PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)

题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候都为0,表明所有的键为坏键. 然后遍历每个字符,统计当前字符连续出现的次数cnt,则只要存在cnt%k!=0,则表明为好键,另其map=1. 最后再for一遍字符串,存储坏键字符串和正确字符串,最后输出即可. #include <iostream> #include <cstdio>

PAT (Advanced Level) 1112. Stucked Keyboard (20)

找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int k;

PAT甲级——A1084 Broken Keyboard

On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen. Now given a string that you are supposed to type, and the string that you actually type out, p

1112 Stucked Keyboard

题意:坏掉的键若被按下,总是重复打出k次.比如,k为3,打出的序列如下-- thiiis iiisss a teeeeeest 坏掉的键是i和e,虽然iiisss中s也出现了3次,但它不是坏掉的键,因为在thiiis中,s值出现了一次,若s是坏掉的键,则每次必须都出现3次. 思路:[字符串处理] 遍历字符串,针对每个字符,统计其连续重复出现的个数(记为len) 1)若len%k==0,说明字符ch可能是坏键.注意,只是可能,并不一定是坏键. 2)若len%≠0,说明字符ch一定是好键. 我是这么

1112 Stucked Keyboard (20分)(字符串)

题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键仅输出一次,数据保证必定存在坏键),接着输出将坏键修好后原本的字符串(K个连续坏键只输出一次). trick: 测试点1答案错误原因:出现次数为K的倍数而不是大于等于K 测试点2,3错误原因:输出原字符串时没有检验最后一段连续相同字符是否为坏键(循环当中没有检验这一段) AAAAAccepted code:

PAT_A1112#Stucked Keyboard

Source: PAT A1112 Stucked Keyboard (20 分) Description: 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 re

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.