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>用来标记一个键是否为坏键,一开始的时候 ...
随机推荐
- SDUT OJ类型转换函数的应用
题目描述 处理一个复数与一个double数相加的运算,结果存放在一个double型变量d1中,输出d1的值.定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operator doub ...
- 51Nod 机器人走方格 V3 —— 卡特兰数、Lucas定理
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1120 题解: 1.看到这种题,马上就想到了卡特兰数.但卡特兰数 ...
- RQNOJ 169 最小乘车费用:水dp
题目链接:https://www.rqnoj.cn/problem/169 题意: 给出行驶1-10公里的费用(所有车一样),可以倒车,问行驶n公里的最小费用. 题解: 大水题... (=´ω`=) ...
- 五年java工作应具备的技能
具有一到五年开发经验 需要学习内容很多 JVM/分布式/高并发/性能优化/Spring MVC/Spring Boot/Spring Cloud/MyBatis/Netty源码分析等等等 01.透彻理 ...
- Zabbix数据库清理历史数据
Zabbix清理历史数据 Zabbix是个很好的监控软件,随着公司监控项目越来越多,数据越来越多,zabbix负载重,可能造成系统性能下降. Zabbix里面最大的表就是历史记录表,history,h ...
- 写个sleep玩玩
static void sig_when_weakup(int no){ printf("weakup weakup\n"); longjmp(buf, ); } void wea ...
- linux 网络编程 inet_pton & inet_ntop函数
#include <arpa/inet.h> int inet_pton(int family,const char * strptr,void * addrptr); 返回:--成功, ...
- CSS实现简单无缝滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- bzoj2673
限制这么多 肯定是网络流 考虑连边 首先我们计算出每行最多放的棋子数$sx[i]$,每列最多放的棋子数$sy[i]$ 首先由源点向第$i$行连流量为$sx[i]$费用为$0$的边,第$i$列向汇点连流 ...
- HihoCoder1673 : 01间隔矩阵([Offer收割]编程练习赛41)(单调队列)
描述 给定一个N × M的01矩阵,小Hi希望从中找到一个01间隔的子矩阵,并且子矩阵的面积越大越好. 例如对于 0101010 1000101 0101010 1010101 0101010 在右侧 ...