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. 简单的贝叶斯分类器的python实现

    # -*- coding: utf-8 -*- ''' >>> c = Classy() >>> c.train(['cpu', 'RAM', 'ALU', 'io ...

  2. 基于bootstrap的文本编辑器组件:Summernote

    Summernote官网地址 :https://summernote.org/ 这是官网的一个例子: <!DOCTYPE html> <html lang="en" ...

  3. mongodb原生node驱动

    写在前面 最近读<node.js学习指南>,对于mongodb没有介绍太多的工作原理,但是对于一个前端开发者,即使你还没有用过这种数据库也可以让你很好的理解和使用       一本非常好的 ...

  4. 大同世界的Java 和.NET 开发

    1.作为一个科班出生的根正苗红的软件开发人员,我认为现在的一群年轻的程序员总是在讨论JAVA  好还是.NET 好的同时,我作为一个做4年开发的.NET 程序员中间穿插了1年JAVA 开发的来说更加的 ...

  5. Convert Application Model Differences

    The eXpressApp Framework is based on the modules concept. As a rule, every module implements a certa ...

  6. SQLMAP学习笔记2 Mysql数据库注入

    SQLMAP学习笔记2 Mysql数据库注入 注入流程 (如果网站需要登录,就要用到cookie信息,通过F12开发者工具获取cookie信息) sqlmap -u "URL" - ...

  7. 单纯形法MATALAB实现

    参考单纯形法的步骤,MATALAB中的实现如下(求极小值): 注:对于极大值的求解,只需要对目标函数添加负号,求解出来的\(X\),再带入原目标函数即可. function [ X, z ] = si ...

  8. Mac下基于testrpc和truffle的以太坊智能合约开发环境搭建

    原文地址:石匠的blog truffle是一个基于Javascript开发的一套智能合约开发框架,使用Solidity语言编写合约.truffle有一套自动的项目构建机制,集成了开发,测试和部署的各个 ...

  9. 1、Ansible安装配置

    ansible介绍: Ansible是一款基于Python开发的自动化运维工具,主要是实现批量系统配置.批量程序部署.批量运行命令.批量执行任务等等诸多功能.Ansible是一款灵活的开源工具,能够很 ...

  10. Nginx 配置优化

    一.开启Gzip 1.参数 gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_comp_level 2;gzip_types text/plain ...