PAT 乙级 1029 旧键盘

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

输入格式:

输入在 2 行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过 80 个字符的串,由字母 A-Z(包括大、小写)、数字 0-9、以及下划线 _(代表空格)组成。题目保证 2 个字符串均非空。

输出格式:

按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有 1 个坏键。

输入样例:

7_This_is_a_test
_hs_s_a_es

输出样例:

7TI

C语言实现

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
int main() {
    char input[81];
    char output[81];
    // 0-25 A-Z, 26-35 0-9 36 _ 
    bool alpha_num[37] = {false};
    scanf("%s", input);
    scanf("%s", output);
    int pos_1, pos_2;
    pos_1 = pos_2 = 0;
    while (input[pos_1] != '\0') {
        // 当字符不相同时,如果字符之前没输出过,输出字符
        while (input[pos_1] != output[pos_2]) {
            // 字符'_'
            if (input[pos_1] == '_' && !alpha_num[36]) {
                alpha_num[36] = true;
                printf("%c", input[pos_1]);
            } else if (isdigit(input[pos_1]) && !alpha_num[input[pos_1] - '0' + 26]) {
                // 字符为数字
                alpha_num[input[pos_1] - '0' + 26] = true;
                printf("%c", input[pos_1]);
            } else if (isalpha(input[pos_1]) && !alpha_num[toupper(input[pos_1]) - 'A']) {
                // 字符为字母
                alpha_num[toupper(input[pos_1]) - 'A'] = true;
                printf("%c", toupper(input[pos_1]));
            }
            ++pos_1;
        }
        // 字符相同并且输出字符未结束,两者同时前进
        if (output[pos_2] != '\0') {
            ++pos_1, ++pos_2;
        } else {
            // 输出字符已经结束,需要把剩余输入字符中没输出过的输出
            while (input[pos_1] != '\0') {
                if (input[pos_1] == '_' && !alpha_num[36]) {
                alpha_num[36] = true;
                printf("%c", input[pos_1]);
                } else if (isdigit(input[pos_1]) && !alpha_num[input[pos_1] - '0' + 26]) {
                    alpha_num[input[pos_1] - '0' + 26] = true;
                    printf("%c", input[pos_1]);
                } else if (isalpha(input[pos_1]) && !alpha_num[toupper(input[pos_1]) - 'A']) {
                    alpha_num[toupper(input[pos_1]) - 'A'] = true;
                    printf("%c", toupper(input[pos_1]));
                }
                ++pos_1;
            }
        }
    }
    return 0;
}

思路

1.使用双指针,分别指向两个字符数组的相应位置,当两者指向的字符不相同时,移动pos_1

2.使用alpha_num去存储已经出现的字符.遇到一个字符,先到alpha_num对应的位置看是否已经访问过,如果没有再输出, 并将对应位置bool值置为true.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值