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 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
分析:字符串题,卡了好久,最后发现样例中的3是输入的数n。。要细心啊!
写的比较复杂,思路如下:
1、建立一个map<char,int>型,先遍历一次,记录每个字母如果重复出现n次,则让该字母所映射的数加n
2、再遍历一次,如果某个char映射的数不为0,那么看他后面n-1位是否相同,如果不相同,则把该数置为0,相当于删除这个“坏键”。
3、输出坏键,注意到按输入顺序输出,并且只能输出一次,这里又建了一个map,代表是否输出过了。如果用set会排序输出,突然想到unordered_set不知道是否也可以,没试过。
4、再按要求输出字符串即可。
复杂度O(len*n),其中len为字符串长度,n为重复次数
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-27-13.17.44
* Description : A1112
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;
unordered_map<char,int> mp;
unordered_map<char,bool> isPrinted;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n,len;
string ori,ans;
cin>>n>>ori;
len = ori.length();
;i<len;i++){
int j;
;j<i+n;j++){
if(ori[i]!=ori[j]){
break;
}
}
if(j==n+i){
mp[ori[i]]+=n;
i+=n-;
}
}
// for(int i=0;i<len;i++){
// if(mp[ori[i]] % n!=0){
// mp[ori[i]]=0;
// }
// }
;i<len;i++){
){
int j;
;j<i+n;j++){
if(ori[i]!=ori[j]){
mp[ori[i]]=;
break;
}
}
;
}
}
;i<len;i++){
&&isPrinted[ori[i]]==false){
cout<<ori[i];
isPrinted[ori[i]]=true;
}
}
cout<<endl;
;i<len;i++){
){
cout<<ori[i];
i+=n-;
}
else cout<<ori[i];
}
;
}
1112 Stucked Keyboard (20 分)的更多相关文章
- 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)
题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...
- PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)
题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候 ...
- PAT (Advanced Level) 1112. Stucked Keyboard (20)
找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...
- 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 (字符串+stl)
此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078 1112 Stucked Keyboa ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...
- 1112 Stucked Keyboard
题意:坏掉的键若被按下,总是重复打出k次.比如,k为3,打出的序列如下—— thiiis iiisss a teeeeeest 坏掉的键是i和e,虽然iiisss中s也出现了3次,但它不是坏掉的键,因 ...
- 【PAT甲级】1084 Broken Keyboard (20 分)
题意: 输入两行字符串,输出第一行有而第二行没有的字符(对大小写不敏感且全部以大写输出). AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #inclu ...
随机推荐
- MyEclipse移动开发教程:设置所需配置的iOS应用(三)
MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] 三.创建配置文件 Provisioning profiles授权文件应用程序在iOS设备上安装并运 ...
- PAIR PROJECTS 附加题
[附加题] 改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. 答:首先,就原有的StopAtEach方法而言,此算法的实现既是非常低 ...
- jQuery 禁用select和取消禁用之disabled
jQuery1.5及以前: 禁用select: $('#groupId').attr('disabled','disabled'); 取消禁用: $('#groupId').removeAttr('d ...
- ImportError: cannot import name 'path'
ImportError: cannot import name 'path' django的版本问题 先卸载 django1.11.14 重新安装最新的django2.0.7
- golang多核的使用
实际上协程只是发生在单个进程内部的,要是想充分的发掘多核CPU的潜力,还是需要多进程的支持. 对于多核编程,go是天生支持,那么我们在什么情况下应该用多核心来加速程序,而在什么情况下用单核即可呢? 现 ...
- https页面证书验证、加密过程简介
1.服务器向CA机构获取证书(假设这个证书伪造不了),当浏览器首次请求服务器的时候,服务器返回证书给浏览器.(证书包含:公钥+申请者与颁发者的相关信息+签名) 2.浏览器得到证书后,开始验证证书的相关 ...
- PLsql登录数据库提示密码即将过期-
小哥询问,PL*SQL用户登录后弹出警告:咋整? ORA-28002:the password will expire within 7 days密码在7天内将到期 do you wish to ch ...
- 【机器学习算法】Boostrapping算法
参考 1.AdaBoost从原理到实现: 完
- hdu1257 dp(最长上升子序列)
题意:有一种拦截系统,可以打击导弹,但是打击的高度会逐渐下降,因此为了防御导弹攻击,就必须用多个系统,现给出一列导弹依次的高度,求最少需要的系统数. 这道题是最长上升子序列问题,但是我一开始其实并没有 ...
- java环境变量 Path 与CLASSPATH
1.Windows操作系统根据Path环境变量来查找命令,Linux操作系统则根据PATH环境变量来查找命令 因为Windows操作系统不区分大小写,设置Path和PATH并没有区别,而Linux系统 ...