PAT-A 1084 Broken Keyboard (20 分) / PAT-B 1029 旧键盘(20分)

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

思路:先将键盘打出来的字符存到一个set<char>  wrong中,set会自动去重,注意将所有的英文字母转换成大写。然后遍历原始字符串,如果发现当前字符在wong中不存在,而且该字符没有输出过,那么输出该字符,并将该字符存到ser<char> found中,表示该字符已经打印过,同样注意所有字母均是大写。使用#include <bits/stdc++.h>导入所有头文件。

#include <bits/stdc++.h>//导入C++所有头文件

using namespace std;

int main()
{
    char good[90];//原始字符串
    char bad[90];//打出来的字符串
    scanf("%s%s", good, bad);

    set<char> wrong;//保存键盘打出来的字符
    for(int i = 0; i < strlen(bad); i++)
    {   //将键盘打印出来的字符存储到一个集合中
        if(isalpha(bad[i]))//英文字母全部存储为大写
            wrong.insert(toupper(bad[i]));
        else
            wrong.insert(bad[i]);
    }

    set<char> found;//已经发现的坏键
    for(int i = 0; i < strlen(good); i++)
    {
        if(isalpha(good[i]))
            good[i] = toupper(good[i]);
        //现在检测的字符不能打印出来,且没有输出过
        if(wrong.find(good[i]) == wrong.end() && found.find(good[i]) == found.end())
        {
            found.insert(good[i]);
            printf("%c", good[i]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38127801/article/details/86514785
今日推荐