【PAT甲级 - C++题解】1084 Broken Keyboard

简介: 【PAT甲级 - C++题解】1084 Broken Keyboard

1084 Broken Keyboard


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
• 1
• 2

Sample Output:

7TI


思路


输入两个字符串 a 和 b ,并且在 b 后面加上 # 作为终止标志。

设置两个指针,i 负责遍历字符串 a ,j 负责遍历字符串 b 。

每次遍历时如果遇到小写字母,统一变成大写字母,这里用到了库函数 toupper ,能够将小写字母转换成大写字母并返回。这样,就得到了两个字符 x 和 y 。

如果 x==y ,则 i 和 j 都往后移一位。

如果 x!=y ,则说明键盘上 x 的地方坏了,如果还没有输出 x 则输出它,然后只有 i 往后移一位。

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a, b;
    cin >> a >> b;
    bool st[200] = { 0 };
    b += '#'; //作为终止标志
    for (int i = 0, j = 0; i <= a.size(); i++)
    {
        char x = toupper(a[i]), y = toupper(b[j]);
        if (x == y)    j++;
        else
        {
            if (!st[x])  cout << x, st[x] = true;
        }
    }
    return 0;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
56 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
74 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
65 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
67 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
73 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
71 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
119 0
|
2天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
42 30
|
16天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)