1112 Stucked Keyboard (20 分)

时间:2023-03-09 10:00:36
1112 Stucked Keyboard (20 分)
1112 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

分析:字符串题,卡了好久,最后发现样例中的3是输入的数n。。要细心啊!

写的比较复杂,思路如下:

1、建立一个map<char,int>型,先遍历一次,记录每个字母如果重复出现n次,则让该字母所映射的数加n

2、再遍历一次,如果某个char映射的数不为0,那么看他后面n-1位是否相同,如果不相同,则把该数置为0,相当于删除这个“坏键”。

3、输出坏键,注意到按输入顺序输出,并且只能输出一次,这里又建了一个map,代表是否输出过了。如果用set会排序输出,突然想到unordered_set不知道是否也可以,没试过。

4、再按要求输出字符串即可。

复杂度O(len*n),其中len为字符串长度,n为重复次数
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-27-13.17.44
 * Description : A1112
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 #include<unordered_map>
 using namespace std;
 unordered_map<char,int> mp;
 unordered_map<char,bool> isPrinted;
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,len;
     string ori,ans;
     cin>>n>>ori;
     len = ori.length();
     ;i<len;i++){
         int j;
         ;j<i+n;j++){
             if(ori[i]!=ori[j]){
                 break;
             }
         }
         if(j==n+i){
             mp[ori[i]]+=n;
             i+=n-;
         }
     }
 //    for(int i=0;i<len;i++){
 //        if(mp[ori[i]] % n!=0){
 //            mp[ori[i]]=0;
 //        }
 //    }
     ;i<len;i++){
         ){
             int j;
             ;j<i+n;j++){
                 if(ori[i]!=ori[j]){
                     mp[ori[i]]=;
                     break;
                 }
             }
             ;
         }
     }
     ;i<len;i++){
          &&isPrinted[ori[i]]==false){
             cout<<ori[i];
             isPrinted[ori[i]]=true;
         }
     }
     cout<<endl;
     ;i<len;i++){
         ){
             cout<<ori[i];
             i+=n-;
         }
         else cout<<ori[i];
     }
     ;
 }