PAT1112:Stucked Keyboard
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 思路 1.用map模拟一个字典dic记录可能卡住的坏键,bool数组用来确定是否是坏键。
2.连续序列中出现次数不小于k的字母可能是坏键,进一步确定如下:
可能出现的情况:
1) 字母key第一次出现时如果连续次数不下于k次可能是坏键,如果接下来key连续出现仍然不小于k次,那么它一定是坏键,反之不是。
2) 如果字母key第一次出现时连续次数小于k次,那么它一定不是坏键,之后不管key连续出现多少次(哪怕超过k)我们也知道它不是坏键,因为那属于正常输出而不是卡键。
3.所以,如果字符串中某个字符只出现了一次超过k次的情况,那么也属于正常范围。 代码
#include<iostream>
#include<vector>
#include<map>
#include<set>
using namespace std;
vector<bool> NotBroken(128,false);//处理已经确认不是坏键但之后出现重复次数超过k的情况
int main()
{
int k,cnt = 0;
string s;
while(cin >> k >> s)
{
map<char,bool> dic; //是否是坏键
set<char> isPrinted;
char prev = '*'; //当前i位置字符的前驱字符
s += '*'; //处理到最后一位都是stuck的情况
for(int i = 0; i < s.size();i++)
{
if(s[i] == prev)
cnt++;
else
{
if(cnt % k != 0)
NotBroken[s[i]] == true;
cnt = 1;
}
if( i != s.size() - 1)
dic[s[i]] = (cnt % k == 0);
prev = s[i];
}
//将已经确认不是坏键但之后出现超过k次的情况排除
for(int i = 0;i < s.size() - 1;i++)
{
if(NotBroken[s[i]])
dic[s[i]] = false;
} //打印坏键,因要按发现的顺序输出,所以不能直接遍历map
for(int i = 0;i < s.size() - 1;i++)
{
if(dic[s[i]] && isPrinted.find(s[i]) == isPrinted.end())
{
cout << s[i];
isPrinted.insert(s[i]);
}
}
cout << endl;
//输出处理后的字符串
for(int i = 0;i < s.size() - 1;i++)
{
cout << s[i];
if(dic[s[i]])
i += k - 1;
} }
}
PAT1112:Stucked Keyboard的更多相关文章
- PAT 1112 Stucked Keyboard
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when yo ...
- 1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- 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 (字符串+stl)
此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078 1112 Stucked Keyboa ...
- PAT_A1112#Stucked Keyboard
Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...
- 【刷题-PAT】A1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- A1112. Stucked Keyboard
On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...
随机推荐
- 如何反编译APK?
1.概述 一些商业的app都包含很多精美的图片还有一些比较好的配置文件,以前某师兄就说过apk把后缀改为zip,然后解压一下就可以获得很多图片资源,但是这时候你打开一下解压出来的xml资源全是乱码.通 ...
- 【翻译】提示18——如何决定ObjectContext的生命周期
原文地址:http://blogs.msdn.com/b/alexj/archive/2009/05/07/tip-18-how-to-decide-on-a-lifetime-for-your-ob ...
- 【面试笔试算法】Problem 8: 然而沼跃鱼早就看穿了一切(hiho题库)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼("marshtom ...
- 安卓TV开发(九) Android模拟事件 遥控器变身成鼠标来操作TV
本文出处:http://blog.csdn.net/sk719887916/article/details/40348853,作者:skay 阅读此文建议先阅读 安卓Tv开发(二)移动智能电 ...
- 【LaTeX排版】LaTeX论文排版<一>
本文及接下来的几篇文章主要讲关于毕设论文的排版. 1.论文的整体构架 学校规定论文字数不得少于15000:说明论文属于中篇论文.一般来说,中长篇论文采用book文类,短篇论文采用article ...
- linux下64位汇编的系统调用(5)
看到这里大家都基本知道了如何进行linux下的汇编系统调用:不过有些童鞋可能会问:那些C库中函数里为我们解决的额外汇编代码你是怎么知道的? 好吧,我承认:我是通过逆向知道的,这貌似有点犯规的嫌疑- 比 ...
- 近期ubuntu 14.04 cpu占用高排障
近期linux使用总是cpu达到满值, 双核cpu其中一个核总是100%,另一个核正常.top之发现输入法框架fcitx满载,直接kill之,发现搜狗输入法不能用了,随即输入如下命令: fcitx f ...
- leetCode之旅(14)-Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- 使用kubeadm搭建Kubernetes(1.10.2)集群(国内环境)
目录 目标 准备 主机 软件 步骤 (1/4)安装 kubeadm, kubelet and kubectl (2/4)初始化master节点 (3/4) 安装网络插件 (4/4)加入其他节点 (可选 ...
- java多继承
众所周知,java面向对象语言中只有单继承的编程语言,也许你会说,通过实现多个接口这种变通的方式达到多继承的目的.没错,你说的对,不过这并不是本片文章要说到的内容,本文要讲到的内容是java中实实在在 ...