pat甲

pat甲 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

解题思路

本题给一条想打印的字符串和实际输出的字符串,问哪些键坏掉了,按原字符串的顺序输出坏掉的键,字母要大写! ①:先接收两串字符串s1,s2,分别用map<char,int> checked,map<char,int> isPut保存可以输出的即完好的键和已经 输出的坏掉的键 ②:先遍历一遍s2,所有小写字母全部变成大写,将所有好的键存储进checked,再遍历s1,同样先小写字母变大写,然后找到 在s1存在但未在checked中存在的键即为坏掉的键,将其输出用isPut保存为已输出即可~~~

AC代码

#include<iostream>
#include<cctype>
#include<map>
using namespace std;

string s1, s2;
map<char, int> checked;
map<char, int> isPut;   //坏掉的键是否已输出

int main()
{
    cin >> s1 >> s2;
    for (int i = 0; i < s2.length();i++)
    {
        if(s2[i]>='a'&&s2[i]<='z')  //小写字母变大写
            s2[i]=toupper(s2[i]);
        checked[s2[i]] = 1;
    }
    for (int i = 0; i < s1.length();i++)
    {
        if(s1[i]>='a'&&s1[i]<='z')
            s1[i]=toupper(s1[i]);
        if(checked[s1[i]]==0)   //坏掉的键
        {
            if(isPut[s1[i]]==0) //未输出过
            {
                cout << s1[i];
                isPut[s1[i]] = 1;
            }
        }
    }
    return 0;
}

本文章使用limfx的vscode插件快速发布