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

本文介绍了PAT编程竞赛中的一道题目——1084BrokenKeyboard,主要解决如何找出坏掉的键盘按键问题。通过比较原始字符串与实际输入字符串,找到不匹配的字符,从而确定哪些键可能已经磨损。文章提供了详细的解题思路和C++代码实现。
摘要由CSDN通过智能技术生成

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1084 Broken Keyboard (pintia.cn)
🔑中文翻译:坏掉的键盘
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

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

Sample Output:

7TI
思路
  1. 输入两个字符串 ab ,并且在 b 后面加上 # 作为终止标志。
  2. 设置两个指针,i 负责遍历字符串 aj 负责遍历字符串 b
  3. 每次遍历时如果遇到小写字母,统一变成大写字母,这里用到了库函数 toupper ,能够将小写字母转换成大写字母并返回。这样,就得到了两个字符 xy
  4. 如果 x==y ,则 ij 都往后移一位。
  5. 如果 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值