PAT A1112 Stucked Keyboard (20 分)——字符串
Posted tccbj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(" "); 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的倍数,再来判断。但总感觉都落下了一个考虑点,一开始认为是坏的,其实后面证明是好的,这个好像都没有考虑。
以上是关于PAT A1112 Stucked Keyboard (20 分)——字符串的主要内容,如果未能解决你的问题,请参考以下文章
PAT (Advanced Level) 1112. Stucked Keyboard (20)