PAT甲级——1112 Stucked Keyboard (字符串+stl)
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
题目大意: 键盘有些按键是坏的,每当按该按键时,对应的字符就会重复输入k的倍数次,保证坏的每一个按键都会重复k的倍数次。现在给定一串用该键盘打出的字符,要求找出坏掉的按键对应的字符,按照遇到的先后顺序不重复地输出,然后再输出原本的字符串。
思路:读取字符串str,第一次遍历字符串str,重复次数不为k的倍数的字符按键确定没坏,用notKey标记(unordered_map),只有重复次数为k的整数倍且未被notKey标记过的字符才是坏掉的按键,用isKey标记。需要注意的是每次标记notKey之后都要对isKey进行标记,不然测试点1就过不掉~~遍历完字符串后要记得对最后一个元素进行判断。
第二次遍历字符串str,借助set将符合要求的元素不重复地按遇到的顺序放入字符数组key,同时将坏掉的按键对应的字符多余的部分在str里替换为‘#’(任意输入中不包含的字符),输出的时候遇到‘#’跳过就行~
这题说难也不难,说不难也卡了我很长时间,比起各种数据结构和算法,我更害怕这种字符串操作的类型,怎么说呢,做出来没有成就感,卡bug又让人奔溃。。。
#include<iostream>
#include<unordered_map>
#include<set>
using namespace std;
unordered_map<char,bool> isKey,notKey;//notKey是确定没问题的按键
set<char> S;
char str[],key[];
int main()
{
int len,k,cnt=,pre=,i;
scanf("%d%s",&k,str);
for(i=;str[i]!='\0';i++){
if(str[pre]==str[i]){
cnt++;
}
else{
if(cnt%k!=){
notKey[str[pre]]=true;
}
else{
if(notKey[str[pre]]){
isKey[str[pre]]=false;
}
else
isKey[str[pre]]=true;
}
if(notKey[str[pre]])//少了这步操作测试点1就过不了
isKey[str[pre]]=false;
cnt=;
}
pre=i;
}
len=i;
if(cnt%k==&&!notKey[str[pre]])
isKey[str[pre]]=true;
if(cnt==){
isKey[str[pre]]=false;
}
cnt=;
i=;
while(i<len){
if(isKey[str[i]]){
if(S.empty()||S.find(str[i])==S.end()){
S.insert(str[i]);
key[cnt]=str[i];
cnt++;
}
int j=i+k,m;
while(str[j]==str[i]&&j<len) j+=k;
for(m=i+(j-i)/k;m<j;m++)
str[m]='#';
i=j;
}
else
i++;
}
key[cnt]='\0';
printf("%s\n",key);
for(i=;i<len;i++){
if(str[i]!='#')
printf("%c",str[i]);
}
printf("\n");
return ;
}
PAT甲级——1112 Stucked Keyboard (字符串+stl)的更多相关文章
- PAT 甲级 1112 Stucked Keyboard
https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...
- PAT甲级 1112 Stucked Keyboard
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 这道题初次写的时候,思路也就是考虑 ...
- 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 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
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when yo ...
- 1112 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 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)
题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...
- PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)
题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候 ...
随机推荐
- linux系统 标准目录及其内容
路径名 操作系统 内容 /bin 所有 最核心的操作系统命令 /boot LS 内核和加载内核所需的文件 /dev 所有 伪终端,磁盘,打印机等的设备项 /etc 所有 关键的启动文件和配置文件 ...
- (2)struts2配置祥解
struts工作流程 反射 : 1.构造对象使用构造器 //类似为Servlet public class AddAction { public AddAction(){ System.out.pri ...
- Linux学习之路(二)文件处理命令之下
分区格式化: 一块分区想要使用的话,要格式化.格式化主要有两个工作,1,把分区分成等大小的数据块,每个数据块一般为4KB.2在分区之前建一个分区表,给第一个文件建一行相关数据,在分区表里保存了它的io ...
- matlab程序计时
t1=datetime(); %程序 t2=datetime() totaltime=t2-t1; disp(t2-t1); 或者: tic %代码块 toc disp(['运行时间: ',num2s ...
- 时尚设计div+css免费模板
时尚设计div+css免费网页模板,时尚设计,div+css. http://www.huiyi8.com/moban/
- LoadRunner中的函数
函数是LoadRunner提供给性能测试工程师的利器,有了它,性能测试工程师可以对脚本进行更为自由的开发,更适应实际测试的需求,进一步扩展脚本的功能. LoadRunner函数的格式: 返回值 函数 ...
- Workerman安装流程
第一步检测安装环境 curl -Ss http://www.workerman.net/check.php | php 操作结果显示 报错了 需要找到php.ini文件 解决办法如下: 打开 php ...
- linux命令学习笔记(32):gzip命令
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间. gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以 ...
- bzoj 3796 Mushroom追妹纸 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3796 先把三个串拼在一起,KMP 求 s1 , s2 中每个位置和 s3 的匹配情况: 注意 ...
- POJ1523(割点所确定的连用分量数目,tarjan算法原理理解)
SPF Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7406 Accepted: 3363 Description C ...