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) 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)的更多相关文章

  1. PAT 甲级 1112 Stucked Keyboard

    https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...

  2. PAT甲级 1112 Stucked Keyboard

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 这道题初次写的时候,思路也就是考虑 ...

  3. PAT甲级——A1112 Stucked Keyboard【20】

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...

  4. PAT 1112 Stucked Keyboard[比较]

    1112 Stucked Keyboard(20 分) On a broken keyboard, some of the keys are always stucked. So when you t ...

  5. PAT 1112 Stucked Keyboard

    1112 Stucked Keyboard (20 分)   On a broken keyboard, some of the keys are always stucked. So when yo ...

  6. 1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  7. 【刷题-PAT】A1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  8. 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)

    题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...

  9. PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)

    题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候 ...

随机推荐

  1. python的模块导入问题

    以下内容参考:http://www.xinxingjiaocheng.com/online/item/7/89 1.给模块起个别名 如果一个模块的名字很长很长,就像这样comput_the_value ...

  2. 「洛谷 P1801」黑匣子

    好像很久没有更过博客了,因为博主这几周很忙.其实是在搞颓. 题意很难懂,所以就不重复了.其实是懒. 一眼看上去这是个 \(Splay\) 裸题,直接插入一个数,查询区间第 \(K\) 大,但是这样太不 ...

  3. 最近火狐浏览器 总是“插件 adobe flash 已崩溃”

    原因和解决方案:在地址栏中输入:about:addons>在如下地方发现firefox已经在警告该插件的安全性了>选择“总不激活”

  4. openfire build(2)

    InterceptorManager PluginManager openfire 插件的中servlet 在web-custom.xml 中的配置 url 一定要小写,访问时不区别大写小 否则404 ...

  5. Java丨角色权限控制——数据库设计

    相信各位读者对于角色权限管理这个需求并不陌生.那么是怎么实现的呢?今天小编来说道说道! 1.首先我们来进行数据库的设计,如何设计数据库是实现权限控制的关键: 1)用户表: id:主键.自增.int n ...

  6. listen 72

    Warmer Temps May Bollux Botanicals Global warming might seem like a botanical boon. After all, milde ...

  7. web前端js过滤敏感词

    web前端js过滤敏感词 这里是用文本输入框还有文本域绑定了失去焦点事件,然后再遍历敏感词数组进行匹配和替换. var keywords=["阿扁","呵呵", ...

  8. 使用zlib实现gzip格式数据的压缩和解压

    注意代码中的注释部分,这里设置是专门针对gzip的,缺少了就不行了,gzip压缩格式和其他格式的区别就在这里. Bytef 就是 unsigned char,uLong就是 unsigned long ...

  9. xxx referenced from: xxx in xxx.o

    情形一:可能是有一些源码文件没有加入工程所导致的,找到相应的.h和.m文件,将其add进入项目工程即可解决这种问题. 情形二:也有可能是某些framework没有加入项目中, 示例:   Undefi ...

  10. Mysql误删了root用户怎么办

    1.停止mysql服务:在mysql安装目录下找到my.ini:在my.ini中找到以下片段[mysqld]:另起一行加入代码:skip-grant-tables 并保存 2.启动mysql服务,并登 ...