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 ;
}

最新文章

  1. 键盘事件触发的兼容tips
  2. Python学习之路【目录】
  3. strlen 字符型数组和字符数组 sizeof和strlen的区别 cin.get(input,Arsize)
  4. 循序渐进Python3(十)-- 2 -- SqlAlchemy
  5. Redis linux 下安装 及扩展配置
  6. Qt拖拽界面 (*.ui) 缩放问题及解决办法(在最顶层放一个Layout)
  7. Android开发者应该深入学习的10个开源应用项目
  8. 如何给开源的DUILib支持Accessibility(论述了DUILib的六个缺点,很精彩)
  9. Windows 8.1 with Update 镜像下载(增OEM单语言版)
  10. Nginx CORS实现JS跨域
  11. Servlet 与 Ajax 交互一直报status=parsererror
  12. python 生成 pptx 分析报告的工具包:reportgen
  13. LFYZ-OJ ID: 1011 hanoi双塔问题
  14. 2018CCPC-女生专场
  15. Java8 中增强 Future:CompletableFuture
  16. Sep 10th 2018
  17. 文件 File 常见操作 工具 MD
  18. Mapreduce实验一:WordCountTest
  19. hdu 1506 Largest Rectangle in a Histogram——笛卡尔树
  20. CentOS简单命令学习:date cal bc

热门文章

  1. 批量导入oracle 的sql脚本
  2. UVA11426 GCD - Extreme (II) —— 欧拉函数
  3. Spring Boot2.0之web开发
  4. myBatis 如何接受 返回count(*),sum()得到的int值
  5. L98
  6. (转)C/C++——auto,static,register,extern用法
  7. linux命令学习笔记(51):lsof命令
  8. BZOJ_2064_分裂_状压DP
  9. 手工创建ASM Disk Groups、为 ASM Disk Groups 添加 disk
  10. dataguard类型转换与模式转化