【刷题-PAT】A1112 Stucked Keyboard (20 分)
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 teeeeeestwe know that the keysiandemight be stucked, butsis not even though it appears repeatedly sometimes. The original string could bethis 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的倍数时,按键一定是好的,置 well[ ] 数组为 true,然后遍历输入的字符串,如果是好的就输出,坏的就记录下来
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<unordered_map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
int k;
scanf("%d", &k);
getchar();
string str;
getline(cin, str);
bool well[260] = {false};
int i = 0, j = 0;
while(i < str.size()){
while(j < str.size() && str[i] == str[j])j++;
if((j - i) % k != 0)well[(int)str[i]] = true;
i = j;
}
string ans1, ans2;
i = 0;
while(i < str.size()){
ans1 += str[i];
if(well[str[i]] == false){
if(ans2.find(str[i]) == string::npos)ans2 += str[i];
i += k;
}else i++;
}
cout<<ans2<<endl;
cout<<ans1<<endl;
return 0;
}
注意:string中的find() 函数的使用
【刷题-PAT】A1112 Stucked Keyboard (20 分)的更多相关文章
- 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 (20)-(map应用)
题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候 ...
- 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)
题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...
- PAT 1112 Stucked Keyboard
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when yo ...
- 【刷题-PAT】A1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT 1112 Stucked Keyboard[比较]
1112 Stucked Keyboard(20 分) On a broken keyboard, some of the keys are always stucked. So when you t ...
- pat 1035 Password(20 分)
1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
随机推荐
- 三、Uniapp+vue+腾讯IM+腾讯音视频开发仿微信的IM聊天APP,支持各类消息收发,音视频通话,附vue实现源码(已开源)-配置项目并实现IM登录
项目文章索引 1.项目引言 2.腾讯云后台配置TXIM 3.配置项目并实现IM登录 4.会话好友列表的实现 5.聊天输入框的实现 6.聊天界面容器的实现 7.聊天消息项的实现 8.聊天输入框扩展面板的 ...
- BitBake使用攻略--BitBake的语法知识一
目录 写在前面 1. BitBake中的赋值 1.1 直接赋值 1.2 间接赋值 1.3 追加与前加赋值 1.4 Override风格的赋值语法 1.5 标志赋值 1.6 内联函数赋值 1.7 其他一 ...
- lldb调试C++总结(3)
note 本文将弥补之前的遗漏部分. continue 前面提到,当设置断点后,使用step和next和finish,程序会停下来,需要程序继续运行,键入continue, 程序可自动继续向下执行. ...
- 【LeetCode】760. Find Anagram Mappings 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决)
本来今天是想做RabbitMQ之优先级队列的,但是,在RabbitMQ Server创建queue时,增加优先级的最大值,头脑发热写了9999999,导致电脑内存直接飙到100%,只能重启电脑,并卸载 ...
- Deep Linear Networks with Arbitrary Loss: All Local Minima Are Global
目录 问题 假设和重要结果 证明 注 Laurent T, Von Brecht J H. Deep linear networks with arbitrary loss: All local mi ...
- PS8625替代方案CS5211|CS5211可以替代兼容PS8625方案|DP转LVDS芯片方案
PS8625|Parade普瑞 PS8625|Parade普瑞 PS8625芯片|Parade普瑞 PS8625方案|Parade普瑞 PS8625芯片代理|DP转LVDS|PS8625替代方案CS5 ...
- Android开发 PorgressBar(进度条)的使用
圆环进度条(默认)和水平进度条: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- 编写Java程序_连锁超市购物结算系统
目录 功能需求: 一.Use Case 1 显示商品信息列表: 二.Use Case 2 输入购买商品编号 三.Use Case 3 显示购物结算清单 需求分级: 实现代码: 功能需求: Soft f ...
- 美和易思 · 「云农职互联网技术学院」HTML+CSS 做西普尼金表官网
假期作业,好久没碰了,代码写得很烂......写博客纯属记录! 源代码下载地址:https://download.csdn.net/download/weixin_44893902/12805555 ...