【PAT甲级】1112 Stucked Keyboard (20分)(字符串)
题意:
输入一个正整数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分)(字符串)的更多相关文章
- PAT甲级——1112 Stucked Keyboard (字符串+stl)
此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078 1112 Stucked Keyboa ...
- PAT 甲级 1112 Stucked Keyboard
https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 On a broken keyboard, ...
- PAT甲级 1112 Stucked Keyboard
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960 这道题初次写的时候,思路也就是考虑 ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...
- PAT甲级——1152.Google Recruitment (20分)
1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 甲级 1042 Shuffling Machine (20 分)(简单题)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
随机推荐
- Feign 不能注入报错及接口参数问题
无法实例 解决方案: @EnableFeignClients(basePackages = "com.test.test.service") 要指定路径, 如果有设置@Compon ...
- 2个月,我从编程小白成为了Python研发工程师
从编程小白,到Python研发工程师,需要多久呢? 答案就是:91门课,450个小时. 听起来似乎难以实现,但其实如果每天抽出八小时学习,两个月的时间,就能由编程小白转变成为Python工程师,听起来 ...
- C++中的参数类型
C++中的参数类型 数组 数组是相同类型数据的集合.引入数组就不需要在程序中定义大量的变量,大大减少程序中变量的数量,使程序精炼,而且数组含义清楚,使用方便,明确地反映了数据间的联系.许多好的算法都与 ...
- Linux之温故知新2
1.关于ssh免密码登陆的ssh-keygen, ssh-copy-id的使用, 然后使用ssh-copy-id user@remote将公钥传给服务器, 以及别名 1 C:\Users\linxmo ...
- HDU1312 Red and Black(dfs+连通性问题)
这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...
- CodeForces 1141B
https://vjudge.net/problem/CodeForces-1141B #include<bits/stdc++.h> using namespace std; int m ...
- centos7添加搜狗输入法
https://www.cnblogs.com/eeexu123/p/9259430.html https://blog.csdn.net/jpch89/article/details/8190380 ...
- NMF: non-negative matrix factorization.
1. 矩阵分解可以用来解决什么方法, 以及how? 利用矩阵分解来解决实际问题的分析方法很多,如PCA(主成分分析).ICA(独立成分分析).SVD(奇异值分解).VQ(矢量量化)等.在所有这些方法中 ...
- js 简单粗暴深拷贝
var obj = {} var newObj = JSON.parse(JSON.stringify(obj))
- Android 开发 微信分享,登陆,获取信息
1 获取appid和appsecret. https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN ...