【PAT】A1084 Broken Keyboard (20分)--简单字符(串)hash

3 篇文章 0 订阅

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

思路总结:

1.思路: 对于输入的两串字符串分别存储,用短字符串做hash,再遍历长字符串对照hash表,把不在hash表的字符输出。
2.易错点:
① 在遍历长字符串与hash表对比时,要分当前字符是大写还是小写。若当前字符在hash表中为false(则该字符为预输出字符)。 且该字符若是大写字母, 则对应的小写字符也必定不在hash表中,则另这一对字符的hash表对应值变为true以避免重复输出; 字符为小写时同理;
②最后输出时要注意小写字母要转换成大写字母,可在遍历时判断出为小写字母时即将其转换为大写。

AC代码

#include<cstdio>
#include<cstring>

bool hash[200] = {false}; //hash表

int main()
{
    char all[82];	//长字符串
    char str[82];	//短字符串

    scanf("%s%s",all,str);
    for(int i=0; i<strlen(str); i++){   //遍历短字符串
        hash[str[i]-'\0'] = true;  //做字符与其对应ASCII码值的hash

    }

    for(int i=0; i<strlen(all); i++){   //遍历长字符串
        char c = all[i];
        int key = c - '\0';
        if(hash[key] == false){     //表示该字符为预输出字符,接下来处理该字符与hash表
            if(c >= 'a' && c <= 'z'){   //若为小写
                hash[c - 'a' + 'A' - '\0'] = true;  //对应大写字母hash表置为true
                c = c - 'a' + 'A';                  //转换为大写以供输出
            }
            else if(c >= 'A' && c <= 'Z'){      //若为大写字母,同理
                hash[c - 'A' + 'a' - '\0'] = true;
            }
            hash[key] = true;           //字符对应的hash值置为true 
            printf("%c",c);             //输出该字符
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值