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, ...
随机推荐
- 【Android 应用开发】Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
- 1.Linux下libevent和memcached安装
1 下载libevent-2.0.22-stable.tar.gz,下载地址是:http://libevent.org/ 2 下载memcached,下载地址是:http://memcached ...
- Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用
1 修改pom.xml,添加依赖文件: <dependency> <groupId>com.whalin</groupId> <artifactId&g ...
- 打包volley
1.如果电脑没有安装git和ant的话,需要安装git和ant,直接Google就可以,并配置环境变量 2.在命令行执行 git clone https://android.googlesource. ...
- 《java入门第一季》之面向对象面试题(this和super的区别)
this和super的区别? 分别是什么呢? this代表本类对象的引用. super代表父类存储空间的标识(可以理解为父类引用,可以操作父类的成员) 怎么用呢? A:调用成员变量 this.成员变量 ...
- g++和gcc的相同点和区别
gcc和g++的区别和联系 gcc和g++都是GNU(一个组织)的编译器. 1.对于.c后缀的文件,gcc把它当做是C程序:g++当做是C++程序: 2.对于.cpp后缀的文件,gcc和g++都会当做 ...
- Android全局异常处理 实现自己定义做强制退出和carsh日志抓取
在做android项目开发时,大家都知道都会遇到程序报错或者Anr异常,会弹出来一个强制退出的弹出框,对于开发人员是好事,但是对于用户体验和 UI实在毫无违和感,别说用户接受不了,就连我们自己本身可能 ...
- 数据结构之---二叉树C实现
学过数据结构的都知道树,那么什么是树? 树(tree)是包含n(n>0)个结点的有穷集,其中: (1)每个元素称为结点(node): (2)有一个特定的结点被称为根结点或树根(root). (3 ...
- rails无法使用页面缓存的解决办法
书上云在config/envirionments/development.rb中开启了缓存机制后,我们即可以使用缓存鸟: config.action_controller.perform_cach ...
- sort list(给链表排序)
Sort a linked list in O(n log n) time using constant space complexity. 题目要求使用O(nlogn)时间复杂度,可以考虑使用归并排 ...