PAT_A1112#Stucked Keyboard

Source:

PAT A1112 Stucked Keyboard (20 分)

Description:

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

Keys:

Code:

 1 /*
 2 Data: 2019-07-19 18:45:30
 3 Problem: PAT_A1112#Stucked Keyboard
 4 AC: 29:33
 5
 6 题目大意:
 7 键盘上坏掉的键会重复打印K次,给出结果字符串,找出坏掉的键和原始字符串
 8 输入:
 9 第一行给出,重复次数k
10 第二行给出,结果字符串
11 输出:
12 坏掉的字符
13 原始字符串
14 */
15
16 #include<cstdio>
17 #include<string>
18 #include<iostream>
19 #include<algorithm>
20 using namespace std;
21 char mp[128]={0};
22
23 int main()
24 {
25 #ifdef    ONLINE_JUDGE
26 #else
27     freopen("Test.txt", "r", stdin);
28 #endif
29
30     int k;
31     scanf("%d", &k);
32     string s,brk="";
33     cin >> s;
34     for(int i=0; i<s.size(); i++)
35     {
36         int cnt=0;
37         while(i+1<s.size() && s[i]==s[i+1]){
38             i++;cnt++;
39         }
40         if((cnt+1)%k==0 && mp[s[i]]==0)
41             mp[s[i]]=1;
42         else if((cnt+1)%k!=0)
43             mp[s[i]]=2;
44     }
45     for(int i=0; i<s.size(); i++){
46         if(mp[s[i]]==1 || mp[s[i]]==3)
47         {
48             if(mp[s[i]]==1){
49                 brk += s.substr(i,1);
50                 mp[s[i]]=3;
51             }
52             s.erase(i,k-1);
53         }
54     }
55     cout << brk << endl;
56     cout << s << endl;
57
58     return 0;
59 }

原文地址:https://www.cnblogs.com/blue-lin/p/11215320.html

时间: 2024-11-02 15:33:53

PAT_A1112#Stucked Keyboard的相关文章

PAT 1112 Stucked Keyboard

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

PAT 甲级 1112 Stucked Keyboard

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 

PAT A1112 Stucked Keyboard (20 分)——字符串

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

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

题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候都为0,表明所有的键为坏键. 然后遍历每个字符,统计当前字符连续出现的次数cnt,则只要存在cnt%k!=0,则表明为好键,另其map=1. 最后再for一遍字符串,存储坏键字符串和正确字符串,最后输出即可. #include <iostream> #include <cstdio>

PAT (Advanced Level) 1112. Stucked Keyboard (20)

找出一定没问题的字符(即一连串的额字符x个数能被k整除的),剩下的字符都是可能有问题的. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int k;

1112 Stucked Keyboard

题意:坏掉的键若被按下,总是重复打出k次.比如,k为3,打出的序列如下-- thiiis iiisss a teeeeeest 坏掉的键是i和e,虽然iiisss中s也出现了3次,但它不是坏掉的键,因为在thiiis中,s值出现了一次,若s是坏掉的键,则每次必须都出现3次. 思路:[字符串处理] 遍历字符串,针对每个字符,统计其连续重复出现的个数(记为len) 1)若len%k==0,说明字符ch可能是坏键.注意,只是可能,并不一定是坏键. 2)若len%≠0,说明字符ch一定是好键. 我是这么

1112 Stucked Keyboard (20分)(字符串)

题意: 输入一个正整数K(1<K<=100),接着输入一行字符串由小写字母,数字和下划线组成.如果一个字符它每次出现必定连续出现K个,它可能是坏键,找到坏键按照它们出现的顺序输出(相同坏键仅输出一次,数据保证必定存在坏键),接着输出将坏键修好后原本的字符串(K个连续坏键只输出一次). trick: 测试点1答案错误原因:出现次数为K的倍数而不是大于等于K 测试点2,3错误原因:输出原字符串时没有检验最后一段连续相同字符是否为坏键(循环当中没有检验这一段) AAAAAccepted code:

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

《linux 内核完全剖析》 keyboard.S 部分代码分析(key_map)

keyboard.S 部分代码分析(key_map) keyboard中间有这么一段,我一开始没看明白,究竟啥意思 key_map: .byte 0,27 .ascii "1234567890-=" .byte 127,9 .ascii "qwertyuiop[]" .byte 13,0 .ascii "asdfghjkl;'" .byte '`,0 .ascii "\\zxcvbnm,./" .byte 0,'*,0,32