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

Ω

寻找所有卡住的按键。首先我们知道每个卡住的按键会重复 次,那么给出一个输入后显示的字符串,请判断哪些字符可能是卡住的(按照出现顺序输出),同时给出正常键盘的输出。

显然,只要一个字符连续出现的次数有一次不是 的倍数,那么它就必然不是卡住的按钮。我们设置一个map<char,short> flag,在遍历字符串的过程中分别代表以下含义:

  1. 表示在之前的字符串中c连续出现的个数都是 的倍数,即是可能卡住的按钮

  2. 表示在之前的字符串中c未出现过(map的初始化值即为0)

  3. 表示c不可能是卡住的按钮(之前的字符串中出现过非 倍数长度的连续c串)

那么局势就明朗多啦,用一个vector<char> stuck来存储怀疑对象,由于不读到最后一个字符就无法肯定stuck中的按钮确实有可能卡住,因此最后在输出stuck时需要检查flag是否为1,-1的不能输出。

遍历整个字符串,如果当前字符与下一个字符不相等(⚠️或是在字符串末尾的字符),就将计数器对 取余,一旦不为0直接-1封杀,如果是 的倍数且 那么就先压入stuck作为考察对象,之前 就不要重复存入stuck了,同时将计数器归0。


🐎

#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
    int k, cnt = 0;
    cin >> k;
    vector<char> stuck;
    map<char, short> flag;
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); ++i)
    {
        cnt += 1;
        if (i < s.size() - 1 && s[i + 1] == s[i]) continue;
        if (cnt % k == 0 && flag[s[i]] == 0)
            stuck.push_back(s[i]), flag[s[i]] = 1;
        else if (cnt % k != 0)
            flag[s[i]] = -1;
        cnt = 0;
    }
    for (auto &c: stuck)
        if (flag[c] == 1) cout << c;
    cout << endl;
    for (int i = 0; i < s.size(); ++i)
    {
        cout << s[i];
        i += (flag[s[i]] == 1) * (k - 1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值