A1112. 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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int hashTB[];
int main(){
char str[], prt[] = {};
int N, K;
fill(hashTB, hashTB + , );
scanf("%d", &K);
scanf("%s", str);
N = strlen(str);
for(int i = ; i < N; i++){
int tag = ;
if(i + K > N){
hashTB[str[i]] = ;
continue;
}
for(int j = i; j < i + K; j++){
if(str[j] != str[i]){
hashTB[str[i]] = ;
tag = ;
break;
}
}
if(tag == ){
i += (K - );
}
}
int flag = ;
for(int i = ; i < N; i++){
if(hashTB[str[i]] == && prt[str[i]] == ){
printf("%c", str[i]);
prt[str[i]] = ;
flag = ;
}
}
if(flag == )
printf("\n");
for(int i = ; i < N; i++){
if(hashTB[str[i]] == ){
printf("%c", str[i]);
}else{
printf("%c", str[i]);
i += K - ;
}
}
cin >> N;
return ;
}
总结:
1、题意:给出一个字符串和粘连次数K,检查这个键是否是坏键。
2、使用哈希表。初始时假设所有键都是坏的,然后遍历,发现a没有粘连,则说明a是好的。 注意不能假设所有键是好的,然后发现坏件再设置为坏。因为aaaba,则会错误将a识别为坏件。
3、注意最后K个字符的判断。
A1112. Stucked Keyboard的更多相关文章
- 【刷题-PAT】A1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- PAT A1112 Stucked Keyboard (20 分)——字符串
On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...
- PAT甲级——A1112 Stucked Keyboard【20】
On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...
- PAT_A1112#Stucked Keyboard
Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...
- 1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- PAT1112:Stucked Keyboard
1112. Stucked Keyboard (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue On a ...
- PAT 1112 Stucked Keyboard
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when yo ...
- PAT 1112 Stucked Keyboard[比较]
1112 Stucked Keyboard(20 分) On a broken keyboard, some of the keys are always stucked. So when you t ...
- PAT甲级——1112 Stucked Keyboard (字符串+stl)
此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078 1112 Stucked Keyboa ...
随机推荐
- 剑指offer(16)栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...
- springboot No Identifier specified for entity的解决办法
今天在做一个项目的时候遇到一个问题,实体类忘了指定主键id,然后报如下错误,也是自己粗心大意造成的,在此记录下. java.lang.IllegalStateException: Failed to ...
- elasticsearch的映射
一.简介: 映射:在创建索引时,可以预先定义字段的类型(映射类型,也就是type,一个索引可以有一个或多个类型)及相关属性. Elasticsearch会根据JSON源数据的基础类型猜测你想要的字段映 ...
- freemarker 设置中文
在web中添加一段代码 <servlet> <servlet-name>freemarker</servlet-name> <servlet-class> ...
- Oracle中保留两位小数
在最近的项目开发中,有个业务需求是界面显示的数字需要保留两位小数,目前我想到的解决方法有两种: (1)在写SQL的时候,直接保留两位小数 (2)在java代码里面将查询出来的数进行格式化处理,保留两位 ...
- U68641 划水(swim.pas/c/cpp)
U68641 划水(swim.pas/c/cpp) 题目背景 小小迪带你划水. 题目描述 原题 输入输出格式 输入格式: 第一行一个数 T. 接下来 T 行每行一个数表示 n 输出格式: 输出 T 行 ...
- maven+Spring+SpringMVC+Hibernate快速搭建
目录结构: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- LVS负载均衡群集
概述 群集的类型:无论是哪种服务器,都至少包括两台节点服务器,而对外表现为一个整体,只提供一个访问入口(域名或IP地址),相当于一台大型计算机.根据群集所针对的目标差异,可以分为以下三个类型: 1.负 ...
- VM磁盘映射共享方法,要求文件系统必须一致
如果主机是window系统,那么虚拟机也应该是Windows系统,不然不起作用
- lyk
/*这道题第一眼看的时候,设f[i]表示1--i的最大空闲时间 但是我们又可以发现,i时刻的最大空闲时间和后面选择任务的持续的时间是有关系的 那么我们就用f[i]来表是i——n的最大空闲时间,即倒着找 ...