PAT 甲级 1112 Stucked Keyboard

Posted zlrrrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
View Code

之前自己写了一个 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;
}
View Code

 

以上是关于PAT 甲级 1112 Stucked Keyboard的主要内容,如果未能解决你的问题,请参考以下文章

PAT_A1112#Stucked Keyboard

PAT A1112 Stucked Keyboard (20 分)——字符串

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

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

1112 Stucked Keyboard (20分)

1112 Stucked Keyboard (20分)