https://pintia.cn/problem-sets/994805342720868352/problems/994805357933608960

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

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + ; int k;
string s; map<char, int> mp, out; struct X {
char sign;
int cnt;
}q[maxn];
int sz; int main() {
cin >> k >> s; int len = s.length(); char Sign = s[];
int Cnt = ; for(int i = ; i < len; i ++) {
if(s[i] == Sign) {
Cnt ++;
} else {
q[sz].sign = Sign;
q[sz].cnt = Cnt;
sz ++; Sign = s[i];
Cnt = ;
}
} q[sz].sign = Sign;
q[sz].cnt = Cnt;
sz ++; /*for(int i = 0; i < sz; i ++) {
cout << q[i].sign << " " << q[i].cnt << endl;
}*/ for(int i = ; i < sz; i ++) {
if(q[i].cnt % k != ) mp[q[i].sign] = ;
} for(int i = ; i < sz; i ++) {
if(mp[q[i].sign] == && out[q[i].sign] == ) {
cout << q[i].sign;
out[q[i].sign] = ;
}
}
cout << endl; for(int i = ; i < sz; i ++) {
int num = q[i].cnt;
if(mp[q[i].sign] == ) num = num / k;
while(num --) cout << q[i].sign;
}
cout << endl; return ;
}

之前自己写了一个 18 的因为没有考虑 sss_s 的情况 这样的情况 s 是不卡的键 所以用 q 记下每一段重复的字符以及出现次数然后再输出答案

(这个是我 wa 掉的代码)

20 分的题目能被我写成这个样子真滴想抽自己了 我是猪吧

#include <bits/stdc++.h>
using namespace std; int N;
string s;
map<char, int> mp;
map<char, int> vis; int main() {
scanf("%d", &N);
cin >> s;
int len = s.length();
mp.clear(); vis.clear(); string ans = "";
string out = "";
for(int i = ; i < len;) {
char temp = s[i];
int cnt = ;
if(mp[temp]) {
out += temp;
i ++;
continue;
}
for(int j = i; j < len; j ++) {
if(s[j] == temp) cnt ++;
else break;
} if(cnt % N == ) {
if(vis[temp] == ) {
ans += temp;
vis[temp] = ;
}
for(int j = ; j < cnt / N; j ++)
out += s[i]; i += cnt;
} else {
mp[s[i]] = ;
out += s[i];
i ++;
}
} cout << ans << endl << out << endl;
return ;
}

PAT 甲级 1112 Stucked Keyboard的更多相关文章

  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 这道题初次写的时候,思路也就是考虑 ...

  3. PAT甲级——A1112 Stucked Keyboard【20】

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

  4. PAT 1112 Stucked Keyboard

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

  5. PAT 1112 Stucked Keyboard[比较]

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

  6. 1112 Stucked Keyboard (20 分)

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

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

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

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

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

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

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

随机推荐

  1. ~ManancherQwQ

    例题传送门 好久没写博客了,写一发. 之前学的Manacher算法拿到模板题找到那最后一个点的错误了,TMD没开long long. Manachar算法主要是处理字符串中关于回文串的问题的,它可以在 ...

  2. BZOJ1068_压缩_KEY

    题目传送门 区间DP,设f[i][j][0/1]为i~j区间的压缩情况,1表示在插入了一个M. code: /********************************************* ...

  3. CF248E Piglet's Birthday

    题面 题意翻译 给定$n$个货架,初始时每个上面有$a[i]$个蜜罐. 有$q$次操作,每次操作形如$u,v,k$,表示从货架$u$上任意选择$k$个蜜罐试吃(吃过的也还能吃),吃完后把这$k$个蜜罐 ...

  4. Openstack入门篇(十三)之云主机创建

    1.知识回顾与端口总结 service 功能 MySQL 为各个服务提供数据存储 RabbitMQ 为各个服务之间通信提供交通枢纽 Keystone 为各个服务器之间通信提供认证和服务注册 Glanc ...

  5. [webpack]--webpack 如何解析代码模块路径

    前言 webpack是如何解析代码模块路径 webpack 中有一个很关键的模块 enhanced-resolve 就是处理依赖模块路径的解析的,这个模块可以说是 Node.js 那一套模块路径解析的 ...

  6. Java编辑PPT的折线图,与内嵌Excel联动

    /** * 折线图的数据写入方法 * @param slide ppt图表 * @param index 折线图的下标 * @param data 需要填充的数据 * @param titles 内嵌 ...

  7. Java子类初始化调用父类无参构造

    实在是服了自己,子类初始化调用父类无参构造这种初学者都应该知道的事,我给忘了. 记得当初看书的时候各种概念抄在笔记本上,再上机实践,以为一辈子都不会忘,还是给忘了. 这件事说明了两个问题: 1.我没有 ...

  8. mac上搭建appium+IOS自动化测试环境(二)

    上一篇: mac上搭建appium+IOS自动化测试环境(一) 9.安装appium-xcuitest-driver依赖 进入WebDriverAgent安装目录,运行bootstrap 首先进入目录 ...

  9. 正确配置 debian squeeze apt 源

    本想在 Debian Squeeze 上安装一些依赖,没想到刚执行 apt-get update ,就出现这样的错误信息. W: GPG error: http://mirrors.163.com s ...

  10. FinTech领域实践:乐维监控助力西南某上市城商行IT运维转型升级!

    FinTech领域实践:乐维监控助力西南某上市城商行IT运维转型升级! 项目背景 随着信息化的逐步深入,企业业务运营活动对IT的依赖程度越来越高,传统的局部.粗放.碎片化的IT运维管理模式已经无法满足 ...