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. 2017-2018-1 20155231 《信息安全系统设计基础》实现mypwd

    2017-2018-1 20155231 <信息安全系统设计基础>实现mypwd Linux pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. p ...

  2. C++实现tar包解析

    tar(tape archive)是Unix和类Unix系统上文件打包工具,可以将多个文件合并为一个文件,使用tar工具打出来的包称为tar包.一般打包后的文件名后缀为".tar" ...

  3. 【BZOJ1045】[HAOI2008]糖果传递

    [BZOJ1045][HAOI2008]糖果传递 题面 bzoj 洛谷 题解 根据题意,我们可以很容易地知道最后每个人的糖果数\(ave\) 设第\(i\)个人给第\(i-1\)个人\(X_i\)个糖 ...

  4. 【BZOJ1050】[HAOI2006]旅行

    [BZOJ1050][HAOI2006]旅行 题面 bzoj 洛谷 题解 先将所有边从小往大排序 枚举钦定一条最小边 再枚举依次枚举最大边,如果两个点联通了就\(break\)统计答案即可 代码 #i ...

  5. 使用web api开发微信公众号,调用图灵机器人接口(二)

    此文将分两篇讲解,主要分为以下几步 签名校验; 首次提交验证申请; 接收消息; 被动响应消息(返回XML); 映射图灵消息及微信消息; 此篇为第二篇. 被动响应消息(返回XML) 上一篇中,我们已经可 ...

  6. yum 执行不了, 解决方法

    在执行yum命令时忽然发现出现以下报错: 1 2 3 4 5 # yum list File "/usr/bin/yum", line 30 except KeyboardInte ...

  7. 什么是Gradle

    一.什么是Gradle 简单的说,Gradle是一个构建工具,它是用来帮助我们构建app的,构建包括编译.打包等过程.我们可以为Gradle指定构建规则,然后它就会根据我们的“命令”自动为我们构建ap ...

  8. WPF阴影效果(DropShadowEffect)(转载)

    <TextBlock Text="阴影效果" FontSize="32"> <TextBlock.Effect> <DropSha ...

  9. C#如何在各类控件中输入\输出数据

    文本框:TextBox Text - 按钮文字 TextBox.text=""; s=TextBox.text; 单选按钮+复选按钮 RadioButton,CheckBox Te ...

  10. WebRtc与SIP

    最近研究一下 webrtc ,看了几篇paper,之前也尝试运行验证了几个demo,现在把我的理解总结到这里. WebRTC 简介 WebRTC,名称源自网页实时通信(Web Real-Time Co ...