【刷题-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 teeeeeest
we know that the keysi
ande
might be stucked, buts
is 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 ...
随机推荐
- CF918B Radio Station 题解
Content 有 \(n\) 个形如 \(a_i.b_i.c_i.d_i\) 的 IP 地址.有 \(m\) 条命令,每条命令由一条字符串 \(s\) 和一个形如 \(p.q.r.s\) 的 IP ...
- CF1119A Ilya and a Colorful Walk 题解
Content 有一个长度为 \(n\) 的数组 \(a_1,a_2,a_3,...,a_n\),试求出两个不相等的数之间的距离的最大值. 数据范围:\(3\leqslant n\leqslant 3 ...
- linux安装软件系列之npm安装
什么是rpm 百度说它是 Red-hat Package Manager (红帽包管理器) 其实它是:RPM Package Manager (RPM包管理器,来源于:https://rpm.org) ...
- java 多线程,单例模式类(创建对象)最优写法
单例模式 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...
- log4j添加日志一定记住在工程的web.xml文件下加一些内容
log4j添加日志一定记住在工程的web.xml文件下加如下内容:
- Dapr项目应用探索
背景介绍 前面文章对Dapr的基本信息进行了学习,接下来尝试将Dapr应用相关应用中. 接下来一步步实现应用dapr功能. 一.预期效果 如上图应用Dapr点包含: a) 报表服务绑定统一数据源服务: ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【九度OJ】题目1018:统计同成绩学生人数 解题报告
[九度OJ]题目1018:统计同成绩学生人数 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1018 题目描述: ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- C# RabbitMQ的使用
本文目的如题. 安装 先说一下RabbitMQ的安装,建议使用Docker镜像安装,Docker安装的好处是不管Windows系统还是Linux,安装步骤少,安装方法相同,不容易出错.使用下面的命令就 ...