题意:

输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成。如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键仅输出一次,数据保证必定存在坏键),接着输出将坏键修好后原本的字符串(K个连续坏键只输出一次)。

trick:

测试点1答案错误原因:出现次数为K的倍数而不是大于等于K

测试点2,3错误原因:输出原字符串时没有检验最后一段连续相同字符是否为坏键(循环当中没有检验这一段)

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
char ans[];
int vis[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k;
cin>>k;
cin.ignore();
string s;
getline(cin,s);
int num=;
int cnt=;
for(int i=;i<s.size();++i){
if(s[i]==s[i-])
++cnt;
else{
if(cnt%k==&&!vis[s[i-]-NULL])
ans[++num]=s[i-],vis[s[i-]-NULL]=;
else if(cnt%k)
vis[s[i-]-NULL]=;
cnt=;
}
}
if(cnt%k==&&!vis[s[s.size()-]-NULL])
ans[++num]=s[s.size()-],vis[s[s.size()-]-NULL]=;
else if(cnt%k)
vis[s[s.size()-]-NULL]=;
cnt=;
for(int i=;i<=num;++i)
if(vis[ans[i]-NULL]==)
cout<<ans[i];
cout<<"\n";
for(int i=;i<s.size();++i){
if(s[i]==s[i-])
++cnt;
else{
if(cnt%k==&&vis[s[i-]-NULL]==){
int flag=;
while(cnt>=k){
cnt-=k;
for(int j=i--flag*k;j>i-k-flag*k;--j)
s[j]='A';
++flag;
}
}
cnt=;
}
}
if(cnt%k==&&vis[s[s.size()-]-NULL]==){
int flag=;
while(cnt>=k){
cnt-=k;
for(int j=s.size()--flag*k;j>s.size()-k-flag*k;--j)
s[j]='A';
++flag;
}
}
for(int i=;i<s.size();++i)
if(s[i]!='A')
cout<<s[i];
return ;
}

【PAT甲级】1112 Stucked Keyboard (20分)(字符串)的更多相关文章

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

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

  2. PAT 甲级 1112 Stucked Keyboard

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

  3. PAT甲级 1112 Stucked Keyboard

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

  4. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  5. PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)

    1050 String Subtraction (20 分)   Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be t ...

  6. PAT甲级——1152.Google Recruitment (20分)

    1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...

  7. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  8. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  9. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

随机推荐

  1. Sql注入之注入点类型和是否存在注入判断

    SQL注入之判断注入类型注入类型分为数字型和字符型和搜索型例如数字型语句:select * from table where id =3,则字符型如下:select * from table wher ...

  2. TODO:rest和restful接口是什么?

    todo: 参考: http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html https://www.jianshu ...

  3. [CF2B] The least round way - dp

    给定由非负整数组成的n×n 的正方形矩阵,你需要寻找一条路径: 以左上角为起点 每次只能向右或向下走 以右下角为终点 并且,如果我们把沿路遇到的数进行相乘,积应当是最小"round" ...

  4. Keep、小红书、美图…独角兽App能拿到新一轮救命钱吗?

    大多数人热爱手机,不是因为时尚的外观或者结实的零部件,而是因琳琅满目的App赋予其太多的功能.智能手机最先是清理掉人类的零碎时间,现如今又开始肢解我们大块的时间,或者说,智能手机本身就是生活.在如此背 ...

  5. STL-map-A - Let the Balloon Rise

    A - Let the Balloon Rise Contest time again! How excited it is to see balloons floating around. But ...

  6. ssm开发垃圾分类查询系统 源码 mysql

    需求 基于SSM实现一个垃圾分类查询管理系统, 用户可以根据自定义查询分类信息, 管理员可以对分类信息, 垃圾详情信息进行增删改查的管理 运行环境 jdk1.8,tomcat8.5,mysql5.6, ...

  7. Mysql备份参数

    --all-databases , -A 导出全部数据库. mysqldump -uroot -p --all-databases --all-tablespaces , -Y 导出全部表空间. my ...

  8. MVC5+EF6入门完整教程6:Partial View

    https://i-beta.cnblogs.com/posts/edit 上篇文章提到过Partial和Action这两个helper, 本篇文章主要就结合这两个helper来讲解分部视图(Part ...

  9. 解决PHP Redis扩展无法加载的问题

    最近在工作中需要使用PHP访问Redis,从https://github.com/phpredis/phpredis下载了phpredis,并且按照官方的说明进行了安装 phpize ./config ...

  10. Android 开发OOM解决方案

    OOM(Out Of Memory)在加载图片过多或者过大的情况下会发生OOM,可以查看APP最高可用内存: int maxMemory = (int) (Runtim.getRuntime().ma ...