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 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<k≤100) 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

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <map>
 5 #include <string>
 6 #include <vector>
 7 #include <queue>
 8 #include <set>
 9 using namespace std;
10 int n,k,m;
11 set<char> good,bad,pr;
12 queue<char> q;
13 int main(){
14     scanf("%d",&n);
15     string s;
16     cin>>s;
17     for(int i=0;i<s.length()-n+1;i++){
18         char now=s[i];
19         int flag=0;
20         for(int j=1;j<n;j++){
21             if(s[i]!=s[j+i]){
22                 good.insert(now);
23                 flag=1;
24                 break;
25             }
26         }
27         if(flag==1){
28             if(bad.find(now)!=bad.end())bad.erase(bad.find(now));
29             continue;
30         }
31         if(good.find(now)==good.end())bad.insert(now),i=i+n-1;
32     }
33     for(int i=0;i<s.length();i++){
34         if(bad.find(s[i])!=bad.end() && pr.find(s[i])==pr.end())printf("%c",s[i]),pr.insert(s[i]);
35     }
36     printf("\n");
37     for(int i=0;i<s.length();i++){
38         if(bad.find(s[i])==bad.end())printf("%c",s[i]);
39         else{
40             printf("%c",s[i]);
41             i=i+n-1;
42         }
43     }
44 }

注意点:看似简单,实际上还蛮复杂的。看了大佬的思路都是看重复的个数是不是n的倍数,再来判断。但总感觉都落下了一个考虑点,一开始认为是坏的,其实后面证明是好的,这个好像都没有考虑。

原文地址:https://www.cnblogs.com/tccbj/p/10438022.html

时间: 2024-10-08 00:15:27

PAT A1112 Stucked Keyboard (20 分)——字符串的相关文章

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

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

1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen. Now given a string that

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often

[PTA] PAT(A) 1008 Elevator (20 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1008 Elevator (20 分) Description  The highest building in our city has only one elevator. A request list is made up with $N$ positive numbers

PAT乙级1088-----三人行 (20分)

1088 三人行 (20分) 输入样例 1: 48 3 7 输出样例 1: 48 Ping Cong Gai 输入样例 2: 48 11 6 输出样例 2: No Solution 思路:1.丙的能力值有可能是小数因此要用double 首次通过代码: 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 6 7 int main(){ 8 int m,x,y; 9 int flag=1; 10 s

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;

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