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, ...
随机推荐
- Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用
1 修改pom.xml,添加依赖文件: <dependency> <groupId>com.whalin</groupId> <artifactId&g ...
- Leetcode_116_Populating Next Right Pointers in Each Node
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43532817 Given a binary tree st ...
- 优雅的App完全退出方案(没有任何内存泄漏隐患)
在Android开发过程中,特别是界面比较多的情况下,用平常的退出方式往往是不能完全退出这个应用,网络上也好多各种退出方案.其中一种应该是被广大开发者采纳使用,也非常的清晰方便,就是在Applicat ...
- Java学习笔记(三)Java2D组件
一 概述 Java2D的一切都基于java.awt包中的Graphics2D类,它是Graphics的子类. 为了绘制图形,需要使用面板作为画布,例如使用JPanel作为画布,面板有一个paintC ...
- Unity Editor 编写unity插件类
在unity写了一个编辑类,基于iTweenpath插件,为了更方便的操作iTweenpath,顺便练习UnityEditor的操作,写了一个CreateiTweenPath,放在Editor文件夹中 ...
- CAS实现单点登录--错误记录
遇到的错误: 生成证书: 1. 命令:keytool -genkey -alias smalllove -keyalg RSA -keystore C:/keys/smallkey 错误:ja ...
- Unity脚本编程之——协程(Coroutine)
本文翻译自Unity官方文档:https://docs.unity3d.com/Manual/Coroutines.html 专有名词: Coroutine 协程 Alpha 不透明度 当你调用一个函 ...
- <<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步(1)
<<操作系统精髓与设计原理>>读书笔记(一) 并发性:互斥与同步 并发问题是所有问题的基础,也是操作系统设计的基础.并发包括很多设计问题,其中有进程间通信,资源共享与竞争,多个 ...
- CSS3概述
首先我们了解下什么是css3,css3是css技术的一个升级.css3中并没有采用总体结构,而是采用分工协作的模块化结构. css3中的模块 模块名称 功能描述 basic box model 定义各 ...
- angularjs系列之轻松使用$q进行异步编程
第一部分关于js中的异步编程 异步编程简单的说就是你写了一段代码,但他不会按照你书写代码的顺序立即执行,而是等到程序中发生了某个事件(如用户点击了某个按钮,某个ajax请求得到了响应)才去执行这段代码 ...