2021-02-03

1084 Broken Keyboard (20 分)
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 you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.

Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _ (representing the space). It is guaranteed that both strings are non-empty.

Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.

Sample Input:
7_This_is_a_test
_hs_s_a_es
Sample Output:
7TI
大致思路:遍历输入串,先假设出现的字符键盘坏了,设置isbroken为true
遍历显示的串,显示过的字符对于的建没坏,故设置isbroken为false
再遍历输入串,isbroken为true的键为没显示出来的字符,如果该字符是小写,转换为大写即可
还有一个注意点,输出要去重,这里我用了unordered_set去重,由于unordered_set没有按插入顺序排列,故不能直接遍历set

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<map>
#include<unordered_set>
using namespace std;
map<char,bool>isbroken;
unordered_set<char>ans;
string input,display;
int main()
{
    
    
    cin>>input>>display;
    for(char x:input){
    
    
        isbroken[x]=true;
    }
    for(char x:display){
    
    
        isbroken[x]=false;
    }
    for(char x:input){
    
    
        if(isbroken[x]==true){
    
    
            if('a'<=x&&x<='z'){
    
    
                x-=32;
                if(ans.insert(x).second==true){
    
    
                    printf("%c",x);
                }
            }
            else{
    
    
                if(ans.insert(x).second==true){
    
    
                    printf("%c",x);
                }
            }
        }
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_45890608/article/details/113601367