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 teeeeeestwe 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

题目大意:给出一个数k和字符串,判断键是否坏了,如果一个字符联系出现K次超过2回,那么被判定为键坏了。输出的时候根据键的发现坏的先后顺序;

非常重要的一点就是:对于普通的按键来说,可能是多按了几次,但是坏的键一定是每按一次重复K次的!

所以比如这样的:

3
fffeee_fffeeeff   那么f一定不是坏的键!因为最后一个只出现了两次!!

这样的:

3
fffeee是f和e均是坏键!

//这个看似简单,写的时候出现了好多问题啊!最大的问题就是怎么按出现的先后顺序输出,如果用map的话,它会按关键字自动排序,使用了unorder_map也是给排序了,我都醉了。

事实证明,无序的map还是排序了:

set也排序的吗??好吧。

//题的难点感觉是如何让这些点按出现的顺序输出,最终的数组里是无记忆的,并不能它们的顺序,

比如 3 eeefff_fffeee此时应该输出ef,而不是fe。

我已经放弃了。

#include <iostream>
#include <cmath>
#include <unordered_map>
#include <set>
using namespace std;
unordered_map<char,int> mp;
bool f[];
int main()
{
int k;
string ch;
cin>>k;
cin>>ch;
int len=ch.size();
set<char> st;
vector<char> vt;
for(int i=; i<len; i++)
{
bool flag=true;
for(int j=i+; j<i+k&&ch[j]!='\0'; j++)
{
if(ch[i]!=ch[j])
{
flag=false;
break;
}
}
if(flag)
{
mp[ch[i]]++;
int id;
if(isdigit(ch[i]))id=ch[i]-'';
else if(isalpha(ch[i])) id=ch[i]-'a'+;
else id=;
if(!f[id]){
vt.push_back(ch[i]);//按照出现的顺序将其放入,那么这样就不能根据下标查找啊。
f[id]=true;
}
//out<<i<<" "<<ch[i]<<'\n';
// if(mp[ch[i]]>1&&st.find(ch[i])==st.end()){
// cout<<ch[i];
// st.insert(ch[i]);
// }
i+=(k-);
}
}
//cout<<'\n';
for(unordered_map<char,int>::iterator it=mp.begin(); it!=mp.end(); it++)
{
if(it->second>)
{
// cout<<it->second<<" ";
//cout<<it->first;
string str;
int pos;
for(int i=; i<k; i++)
str+=it->first;
while(ch.find(str)!=string::npos)
{
pos=ch.find(str);
ch.erase(pos,k-);
}
}
}
cout<<'\n'<<ch;
return ;
}
/***
3
feeeefe
3
eeeffffffeee
***/

//写成了这样 ,还是决定放弃,不知道怎么记录它们的顺序。

大佬代码:https://blog.csdn.net/kakitgogogo/article/details/52108461

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
using namespace std; int main()
{
int k;
string s;
cin>>k>>s;
int n=s.size();
bool isprob[];
memset(isprob,true,sizeof(isprob));
set<char>res;
for(int i=;i<n;)//这里对i的操作没有写,下边是直接对i赋值,没有见过这样的,学习了。
{
int j=i+,count=,idx;
if(isdigit(s[i])) idx=s[i]-'';
else if(isalpha(s[i])) idx=s[i]-'a'+;
else idx=;
while(s[j]==s[i]&&j<n&&count<k)
{
count++;
j++;
}
if(count==k&&isprob[idx])
{
res.insert(s[i]);
}
else if(count!=k)
{
isprob[idx]=false;
//但是这个集合并没有表示所有的。
//如
res.erase(s[i]);///在集合中删除也是这样。
}
i=j;//不管是否是重复的那么i=j,这样就实现了对i的更新了。
} set<char>res2=res;///可以直接赋值创建的。
for(int i=;i<n;i++)
{
if(res2.find(s[i])!=res2.end())///原来是通过将集合中的不变,去从头遍历啊! {
cout<<s[i];//这样是一定可以保证是按顺序来的。
res2.erase(s[i]);
}
if(res2.empty()) break;
}
cout<<endl;
for(int i=;i<n;)
{
if(res.find(s[i])!=res.end())///如果能找得到,那么就输出,简直太厉害了。
{
cout<<s[i];
i+=k;
}
else
{
cout<<s[i];
i++;
}
}
cout<<endl;
}
/***
3
fffeee ***/

1.使用set来存,如果后来不相等,那么就从set中删除,并标记为false;

2.顺序的话是通过顺序遍历字符串得到的,遍历过一次就从set中删除;

3.其实这个id可以用map来表示的。

PAT 1112 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. PAT甲级——1112 Stucked Keyboard (字符串+stl)

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

  3. 1112 Stucked Keyboard (20 分)

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

  4. PAT 甲级 1112 Stucked Keyboard

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

  5. PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)

    题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候 ...

  6. PAT (Advanced Level) 1112. Stucked Keyboard (20)

    找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include ...

  7. 【PAT甲级】1112 Stucked Keyboard (20分)(字符串)

    题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键 ...

  8. PAT甲级 1112 Stucked Keyboard

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 这道题初次写的时候,思路也就是考虑 ...

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

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

随机推荐

  1. Win7,Win8安装ArcGIS软件或Node.js等安装包出现2503错误的解决方法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZXNyaWNoaW5hY2Q=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. QQ第三方登录实例demo(QQSDK包优化)

    实现效果: 实现流程: 1.注冊QQ互联开发人员 QQ互联官网 注冊成为开发人员(须要审核) 2.审核通过之后 申请应用(须要互联人员审核*须要备案成功的线上域名) 以下我们開始下载QQsdk包 QQ ...

  3. svn 操作字母的提示

    今天使用SVN提交代码,发现提交后的代码找不到之前的版本. 操作的字母缩写为R.一般我们常见的操作为 A D M R   A:add,新增 C:conflict,冲突 D:delete,删除 M:mo ...

  4. testNG框架提示:Cannot find class in classpath: NewTest

    selenium+Java的testNG运行时,报如下错误: org.testng.TestNGException: Cannot find class in classpath: NewTest a ...

  5. Android之HttpPost与HttpGet使用

    一)HttpGet :doGet()方法 //doGet():将参数的键值对附加在url后面来传递 public String getResultForHttpGet(String name,Stri ...

  6. Kafka中Producer端封装自定义消息

    我们知道KeywordMessage就是被kafka发送和存储的对象.所以只需要模拟出这个就可以发送自定义消息了. 比如我需要将用户的id,user,age,address和访问ip和访问date记录 ...

  7. raw_input()

    raw_input() 用于接收标准输入,并把标准输入当成字符串类型来处理,只能在 Python2 中使用,Python3 中没有这个函数 #!/usr/bin/env python #-*- cod ...

  8. 【Thinkphp5 】部署nginx时nginx.conf配置文件修改

    背景:thinkphp5项目 服务器环境: lnmp 1 打开路径  /usr/local/nginx/conf/vhost/ 此路径下会有你添加的域名文件夹..找到对应的域名打开. 2  代码如下, ...

  9. hibernate实现有两种配置,xml配置与注释配置。<转>

    <注意:在配置时hibernate的下载的版本一定确保正确,因为不同版本导入的jar包可能不一样,所以会导致出现一些错误> hibernate实现有两种配置,xml配置与注释配置. (1) ...

  10. redis未授权访问漏洞那拿SHELL

    一.什么是redis未授权访问漏洞: 1.redis是一种文档型数据库,快速高效,存储在内存中,定期才会写磁盘.主要用于快速缓存,数据转存处理等.默认redis开在6379端口,可以直接访问.并不需要 ...