1084 Broken Keyboard
Posted CSU迦叶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1084 Broken Keyboard相关的知识,希望对你有一定的参考价值。
两个注意的点
1.本题被归到散列专题下,但是由于是逐字符地映射到整形,可以直接把布尔型哈希数组的大小设置为ASCII的数量128,然后直接将字符作为数组下标(如果是字符串,才需要自己写一个哈希函数,将字符串映射到整形,对于本题是不需要的,多此一举使得代码复杂更容易出错)
2.其次是输出的时候,就算是小写字母的键盘坏了,也输出大写字母,那么小写字母什么时候转化成大写字母(即减去32)就很重要。如果在判断字母有无出现之前,就转换,那么就错了。因为a没出现过,A出现过的时候,就会漏掉输出。
以下是AC代码
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 128;
const double eps = 1e-3;
int main(){
// 7_This_is_a_test
// _hs_s_a_es
// 7TI
char readyStr[maxn] = "";
char typeoutStr[maxn] = "";
scanf("%s %s",readyStr,typeoutStr);
int readyLen = strlen(readyStr);
int typeoutLen = strlen(typeoutStr);
bool appearHash[maxn] = {false};//有无出现过
bool outputHash[maxn] = {false};//有无输出过 --不要小写字母了,前26位一定是false
for(int i=0;i<typeoutLen;i++){
appearHash[typeoutStr[i]] = true;
}
for(int i=0;i<readyLen;i++){
char nowCh = readyStr[i];
if(!appearHash[nowCh]){
if(nowCh>='a'&&nowCh<='z')nowCh -= 32;
if(!outputHash[nowCh]){
printf("%c",nowCh);
outputHash[nowCh] = true;
}
}
}
return 0;
}
以上是关于1084 Broken Keyboard的主要内容,如果未能解决你的问题,请参考以下文章
PAT甲级1084 Broken Keyboard (20 分)