zoukankan      html  css  js  c++  java
  • 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

    代码:

    #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 记下每一段重复的字符以及出现次数然后再输出答案

    (这个是我 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
  • 相关阅读:
    java线程池,工作窃取算法
    java线程池,阿里为什么不允许使用Executors?
    CMakeLists 的使用,大型工程使用cmake 的构件过程
    ieee文献免费下载办法
    欧式距离、标准化欧式距离、马氏距离、余弦距离
    sliding window:"Marginalization","Schur complement","First estimate jacobin"
    机器学习中的线性代数之矩阵求导
    opencv中滤波方法学习
    opencv关于Mat类中的Scalar()---颜色赋值
    C/C++预处理指令#define,#ifdef,#ifndef,#endif…
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10301227.html
Copyright © 2011-2022 走看看