题意:

输入一个正整数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. 小I的小姐姐

    小 I 的小姐姐 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小 I 去天津玩啦,一路上,他跟他的同学发生了许多有趣 ...

  2. [CF269B] Greenhouse Effect - dp

    给出 N 个植物,每个植物都属于一个品种,共计 m 个品种,分落在不同的位置上(在一个数轴上,而且数轴是无限长度的),保证读入的位置是按照升序读入的. 现在我们可以进行一个操作:取任意一个位置上的植物 ...

  3. TD - SimpleTextarea

    html模板 <input dojoType="bootstrap.form.SimpleTextarea" dojoAttachPoint="assetDescr ...

  4. android TextView 支持长按自由复制

    因为EditText支持系统的长按自由复制,所以只需要把EditText通过配置达到TextView效果就行了 <EditText android:id="@+id/subject_i ...

  5. C++-CodeForces-1313A

    真的打起比赛来,连个贪心都写不好,呜呜呜. #include <bits/stdc++.h> using namespace std; ],t,ans; void IF(int&a ...

  6. 第一篇 网站基础知识 第4章 Java中Socket的用法

    第4章 Java中Socket的用法 4.1 普通Socket的用法 Java中的网络通信是通过Socket实现的,Socket分为ServetSocket和Socket两大类,ServetSocke ...

  7. 安装php-zbarcode的步骤方法

    1 安装ImageMagick依赖 yum install ImageMagick ImageMagick-devel 2 安装zbar拓展 wget -c http://jaist.dl.sourc ...

  8. uevent机制

    uevent, user space event. 内核与用户空间的一种通信机制,基于netlink机制,主要用于设备驱动模型,例如热插拔. 1.调用/sbin/mdev的流程分析 在驱动程序中经常出 ...

  9. 未能加载文件或程序集“Autofac.Integration.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    是因为web.config中dependentAssembly结点下的版本号和当前引用的程序集的版本号不一致!

  10. IntelliJ IDEA 2017.3百度-----文件树状结构