【PAT甲级 - C++题解】1112 Stucked Keyboard

简介: 【PAT甲级 - C++题解】1112 Stucked Keyboard

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 k (1<k≤100) 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


题意

因此,当你用该键盘输入一些句子时,与这些键相对应的字符将在屏幕上重复出现 k 次。


现在,给定 k 以及最终屏幕显示的结果字符串,请你找出所有可能坏掉的按键,并给出原始字符串。


例如,当 k=3 时,从字符串 thiiis iiisss a teeeeeest,我们可以推断出 i 和 e 可能被卡住了,因为 i 出现在两个地方,都是连续出现 3 次,能被 k 乘除,而 e 连续出现了 6 次,同样也能被 k 整除。但是 s 并没有被卡住,因为 thiiis 中 s 只出现了 1 次,其长度不能被 k 整除。


所以,原始字符串可能是 this isss a teest。


思路

我们可以用一个 st 数组来记录每个字符是否被卡住。


首先,遍历一遍字符串,筛选出没被卡住的字符,即字符出现的连续长度不能被 k 整除就没有被卡住,而最后剩下没有被筛选出的字符就是被卡住了。


然后再遍历一遍字符串,通过上次遍历得到的 st 数组中的结果输出相应被卡住的字符,并记录原始字符串的结果,然后最后输出。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int st[N];
int main()
{
    int k;
    string str;
    cin >> k >> str;
    //先把没卡住的字符筛选出来,剩下的字符就是卡住的
    for (int i = 0; i < str.size(); i++)
    {
        int j = i + 1;
        while (j < str.size() && str[j] == str[i]) j++;    //找出连续字符
        int len = j - i;
        if (len % k)   st[str[i]] = 1;   //如果长度不能整除k,则该字符没卡住
        i = j - 1;
    }
    //输出卡住的字符
    string res = "";
    for (int i = 0; i < str.size(); i++)
    {
        if (!st[str[i]]) cout << str[i], st[str[i]] = 2;
        if (st[str[i]] == 1)   res += str[i];
        else
        {
            res += str[i];
            i += k - 1;
        }
    }
    cout << endl << res << endl;
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
187 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
229 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
226 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
217 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
130 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
152 0
|
10月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
6月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
175 0
|
6月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
269 0
|
8月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
310 12