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. c++ 文件共享打开

     _fsopen参数说明  #include<share.h>  _fsopen 共享模式访问文件 //安全性比fopen高 _fsopen 以共享的方式打开文件或者流 FILE * ...

  2. P1402 酒店之王

    P1402 酒店之王 每个人要匹配一个A和一个B,所以这样连边: S向每个房间连边. 每个房间向喜欢这个房间的人连边. 每个人向喜欢的菜连边. 每道菜向T连边. 边权均为1. 注意人要限流. // I ...

  3. selenium 定位不到元素总结

    元素在网页上,却会出现定位不到的情况的分析. 1. 定位不正确. 2. 页面还没有加载完就去查找元素了. 3. 有遮罩层. 首先说下第3点. 先前在公司遇到过这样的问题. 页面是显示出来了, 这个元素 ...

  4. idea_debug

    条件断点 快捷键 cmd + shift +f8 demo 表达式求值 注意,调试的时候,选中相应变量 alt + f8 demo set value (感觉会非常有用) 调试时直接改变变量的值,快捷 ...

  5. 【转】利用telnet来进行调试Skynet

    https://blog.csdn.net/WhereIsHeroFrom/article/details/80674408

  6. 32bit GM命令

    GM 命令[32位服务端GM命令] //announce message this basicly tells a announcement on the whole server.. you can ...

  7. 3分钟手把手带你搭建基于selenium的自动化框架

    1 .什么是seleniumSelenium 是一个基于浏览器的自动化工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.Sel ...

  8. 富文本(wangEditor框架)的使用教程

    富文本的使用教程(wangEditor框架) 一,相信很多人用过很多富文本的框架,现在我向大家推荐一个很实用的一个富文本框架,具有丰富的功能项,容易使用. 所以本博客介绍这个富文本编辑器的使用哈!觉得 ...

  9. ossec安装

    安装 安装要求 对于Unix系统来说,OSSEC只需要GNU的make.gcc和libc.推荐使用OpenSSL,但仅属于一个可选项.而且,通常您只需在一个系统上做编译操作,然后将二进制程序复制到其他 ...

  10. uptime命令详解

    基础命令学习目录首页 users个数和窗口数一致 原文链接:https://www.cnblogs.com/ultranms/p/9253217.html uptime 另外还有一个参数 -V(大写) ...