码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-02-26 16:57:24      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:UNC   ast   class   line   end   pre   display   hid   result   

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<k100) 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 }
View Code

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

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

标签:UNC   ast   class   line   end   pre   display   hid   result   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!