PAT甲级1084(坏键盘打字)

1084 Broken Keyboard (20 point(s))

  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

题目大意

  现有一块某些键已经坏掉的破键盘。所以当你键入一些句子的时候,某些字符可能打不出来。现在给出一串你先输入的字符串以及你最终键入的字符串,请找出那些坏掉的键

解题思路

  首先建立两个hash数组,hashTable1表示坏键,hashTable2表示没有坏的键。读入str,之后依次读入最终输出的字符,在hashTable2相应位置置1,再遍历str,如果该字符对应位置hashTable2中为0,则说明该键坏了,在hashTable1中相应位置置1。之后遍历str,如果字符在hashTable1相应位置的值为1,则输出,并将该位置置0。注意:若为字母,则输出大写

代码

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;

int main()
{
	char str[85];
	int hashTable1[128] = { 0 }, hashTable2[128] = { 0 };//hashTable1表示坏键,hashTable2表示没有坏的键
	int ret = scanf("%s", str);
	ret = getchar();
	char c;
	while ((c = getchar()) != '\n') {//最终打出来的字符
		if (islower(c)) c -= 32;//小写-->大写
		hashTable2[c - '0'] = 1;
	}
	for (int i = 0; i < strlen(str); ++i) {
		if (islower(str[i])) str[i] -= 32;
		if (hashTable2[str[i] - '0'] == 0) {//坏键,在hashTable1相应位置置1
			hashTable1[str[i] - '0'] = 1;
		}
	}
	for (int i = 0; i < strlen(str); ++i) {
		if (hashTable1[str[i] - '0'] == 1) {//坏键输出一次后将相应位置置0
			printf("%c", str[i]);
			hashTable1[str[i] - '0'] = 0;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值