1112. Stucked Keyboard (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. PAT 1112 Stucked Keyboard

    1112 Stucked Keyboard (20 分)   On a broken keyboard, some of the keys are always stucked. So when yo ...

  2. 1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  3. PAT 1112 Stucked Keyboard[比较]

    1112 Stucked Keyboard(20 分) On a broken keyboard, some of the keys are always stucked. So when you t ...

  4. PAT甲级——1112 Stucked Keyboard (字符串+stl)

    此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078   1112 Stucked Keyboa ...

  5. PAT_A1112#Stucked Keyboard

    Source: PAT A1112 Stucked Keyboard (20 分) Description: On a broken keyboard, some of the keys are al ...

  6. 【刷题-PAT】A1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  7. A1112. Stucked Keyboard

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...

  8. PAT A1112 Stucked Keyboard (20 分)——字符串

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...

  9. PAT 甲级 1112 Stucked Keyboard

    https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...

随机推荐

  1. APPCORE Routine APIs

    Introduction to APPCORE Routine APIs This chapter provides you with specifications for calling many ...

  2. 用C语言实现Ping程序功能

    本文转载自:http://www.ibm.com/developerworks/cn/linux/network/ping/ ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具.p ...

  3. 新IO建立的聊天程序

    服务端: package com.net.scday3; import java.io.IOException; import java.net.InetSocketAddress; import j ...

  4. 第十一章 图像之2D(1)SpriteBatch

    Android游戏开发群:290051794 Libgdx游戏开发框架交流群:261954621 作者:宋志辉  出处:http://blog.csdn.net/song19891121 本文版权归作 ...

  5. LAV Filter 源代码分析 4: LAV Video (2)

    上一篇文章分析了LAV Filter 中的LAV Video的两个主要的类:CLAVVideo和CDecodeThread.文章:LAV Filter 源代码分析 3: LAV Video (1) 在 ...

  6. sqlserver2008中删除了windows用户,导致无法登陆的解决方案

    打开管理工具中的"服务",找到并关闭SQL Server服务.进入命令进入SQL Server 2008的安装目录,找到sqlservr.exe文件,执行命令:sqlservr - ...

  7. Android实训案例(三)——实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果!

    Android实训案例(三)--实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果! 感叹离春节将至,也同时感叹时间不等人,一年又一年,可是我依然是android道路上的小菜鸟,这篇讲 ...

  8. Unity Socket TCP

    using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Net.Sock ...

  9. The 6th tip of DB Query Analyzer

      The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...

  10. Mac OS X下各种文件编码的转换方法

    何曾几时本猫还在windows下编码的时候,那时ruby的源代码的编码格式都是gbk啊!导致N多中文显示为乱码.后来无奈写了个转换代码从gbk编码转为utf-8格式的小工具: #!/usr/bin/r ...