【PAT】1112. Stucked Keyboard (20)【字符串处理】

2024-04-12 06:08

本文主要是介绍【PAT】1112. Stucked Keyboard (20)【字符串处理】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

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.

翻译:在一个坏掉的键盘上,有些键总是被卡住。所以当你输入一些句子时,这些键对应的字符会在屏幕上重复出现k次。
现在,给定屏幕上的结果字符串,你需要列出所有可能的键和原始字符串。
注意,可能有一些字符是重复输入的。无论何时按下,被卡住的键总会重复输出固定的k次。例如,当k=3时,从字符串thiiis iiisss a teeeeeest中我们知道键i和e可能被卡住,但s不会,尽管它有时会重复出现。原始的字符串可以是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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行给出一个正整数k(1<k≤100) ,代表卡住的键的重复次数。第二行包含屏幕上的结果字符串,它由不超过1000个来自{a-z}、{0-9}和_的字符组成。数据保证字符串是非空的。

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次则记为-1,代表不是卡住的键。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int k;
char s[1010];
bool p[1010];
char ans[50],anscount=0;
int v[128];
int vc[128];
int main(){scanf("%d\n%s",&k,s);int length=strlen(s);int ccount=0;char c=' ';for(int i=0;i<length;i++){if(c!=s[i]){if(ccount>0)v[c]=-1;c=s[i];ccount=1;}else{ccount++;if(ccount==k){if(v[c]!=-1)v[c]=1;ccount=0;}}}ccount=0;c=' ';for(int i=0;i<length;i++){p[i]=true;if(c==s[i]){if(v[c]==1){ccount++;p[i]=false;	if(!vc[c]){ans[anscount++]=c;vc[c]=1;}			}if(ccount==k){ccount=0;c=' ';}}else{c=s[i];ccount=1;}}for(int i=0;i<anscount;i++){printf("%c",ans[i]);}printf("\n");for(int i=0;i<length;i++){if(p[i])printf("%c",s[i]);}printf("\n");return 0;
}

这篇关于【PAT】1112. Stucked Keyboard (20)【字符串处理】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896312

相关文章

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer