【PAT】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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括起始字符串,第二行为实际打出的字符串。每个字符串不超过80个字符,仅包括英文字母 [A-Z] (大小写不敏感),数字[0-9],或“_”(代替空格)。保证所有字符串不为空。

OUTPUT FORMAT

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


解题思路

通过Hash计算出原字符串减去匹配字符串后的各字符数量,如果大于0则输出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int Hash[256];
int main(){
    string s,p;
    cin>>s;
    cin>>p;
    for(int i=0;i<s.size();i++){
        if(s[i]>='a'&&s[i]<='z')s[i]=s[i]-'a'+'A';
        Hash[s[i]]++; 
    }
    for(int i=0;i<p.size();i++){
        if(p[i]>='a'&&p[i]<='z')p[i]=p[i]-'a'+'A';
        Hash[p[i]]--;
    }
    for(int i=0;i<s.size();i++){
        if(Hash[s[i]]>0){
            printf("%c",s[i]);
            Hash[s[i]]=0;
        }
    }
    printf("\n");
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值