PAT B1033 旧键盘打字 (20 分)

75 篇文章 0 订阅

PAT B1033 旧键盘打字 (20 分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?

输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10^ ​5​​ 个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。

注意:如果上档键坏掉了,那么大写的英文字母无法被打出。

输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。

输入样例:
7+IE.
7_This_is_a_test.
输出样例:
_hs_s_a_tst
以下是AC的代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 100100;
bool hashtable[256];
char str[maxn];

int main(void){
	memset(hashtable,true,sizeof(hashtable));
	cin.getline(str,maxn);
	int len = strlen(str);
	for(int i=0;i<len;i++){
		if(str[i]>='A' && str[i]<='Z'){
			int low = str[i]-'A'+'a';
			hashtable[low]=false;
		}
		hashtable[str[i]]=false;
	}
	cin.getline(str,maxn);
	len=strlen(str);
	bool printed=false;
	for(int i=0;i<len;i++){
		if(str[i]>='A'&&str[i]<='Z'){
			int low = str[i]-'A'+'a';
			if(hashtable[low]&&hashtable['+']){
				printf("%c",str[i]);
				printed=true;
			}
		}else if(hashtable[str[i]]){
			printf("%c",str[i]);
			printed=true;
		}
	}
	if(!printed) printf("\n");
	return 0;
} 

思路:
常规散列题,但是这道题有个需要注意的地方,就是题目里只说了保证第二行非空。
实测如果使用scanf函数,第三个测试点无论如何都无法通过,算法笔记上面是用的gets(),这已经被禁用了。改成cin.getline(str,maxn)之后才通过了。以下是在查找过程中看到的别人的代码,觉得写得非常好,可供参考:

#include <iostream>
#include <cctype>
using namespace std;
int main() {
  string bad, should;
  getline(cin, bad);
  getline(cin, should);
  for (int i = 0, length = should.length(); i < length; i++) {
    if (bad.find(toupper(should[i])) != string::npos) continue;
    if (isupper(should[i]) && bad.find('+') != string::npos) continue;
    cout << should[i];
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值