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. zabbix 上 mysql 优化

    摘自: https://segmentfault.com/a/1190000001638101

  2. JAVA-三大语句(选择语句、条件语句、循环语句)

    跳出指定的for循环体,和goto很像 1 K:for(int i=0;i<3;i++){//给这个for循环体取一个名字为K 2 for(int j=0;j<3;j++){ 3 if(j ...

  3. timing-function: steps()

    animation语法 animation:name duration timing-function delay iteration-count direction timing-function取 ...

  4. BZOJ3991:寻宝游戏 (LCA+dfs序+树链求并+set)

    小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路上行走 ...

  5. kettle每天自动发送邮件总结_20161128

    kettle作为java开发的工具,很多功能在目前工作中还用不到,原来它也是支持java代码的,现在用到的也就是它从服务器导数到数据库,然后再进行数据处理的功能. 如何快速学会使用kettle发送邮件 ...

  6. 创建maven多模块项目

    一:创建父项目

  7. <十八>UML核心视图动态视图之协作图

    一:协作图 --->描述了对象间交互的一种模式.它通过对象之间的连接和它们相互发送的消息来显示参与交互的对象 --->协作图可以有对象和主角实例,以及描述它们之间关系和交互的连接和消息.通 ...

  8. ACM学习历程—HDU 5326 Work(树形递推)

    Problem Description It’s an interesting experience to move from ICPC to work, end my college life an ...

  9. ACM学习历程—HDU1392 Surround the Trees(计算几何)

    Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these ...

  10. 洛谷 P1496 火烧赤壁

    题目描述 曹操平定北方以后,公元208年,率领大军南下,进攻刘表.他的人马还没有到荆州,刘表已经病死.他的儿子刘琮听到曹军声势浩大,吓破了胆,先派人求降了. 孙权任命周瑜为都督,拨给他三万水军,叫他同 ...