PAT甲级刷题之路——A1084 Broken Keyboard


看题目估计是字符串模拟了

原题如下

在这里插入图片描述
在这里插入图片描述

题目大意

在键盘上有一些键坏了,所以当想大一些字符的时候不会显示在屏幕上,给出你想编辑的话和显示在屏幕上的话,找出哪些键打不出字符。
每一个字符串不超过80个字符其中’_'代表空格,英语字符必须大写capitalized

自己的想法

  1. 把数字和字母分别储存在数组中,先扫描被破坏的字符串,若出现的字符则在数组中标记
  2. 再扫描原始字符串,未在数组中标记的则输出并标记已经输出了(注意输出字母为大写)

答案反馈

错误1

在这里插入图片描述
行吧,只对了一个测试点竟然还能拿到超过一半的分,平均下来其他4个测试点一个2分,大概都是特殊点?
发现一个,就是空格我没有考虑,竟然这个测试点只值一分?!!

错误2

在这里插入图片描述
先放一下自己繁琐的代码,实际上用string.find()就可以解决,明天debug~

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<queue>
#include<map>
#include<stack>
using namespace std;
int alpha[30] = { 0 };
int num[10] = { 0 };
int _ = 0;
int main() {
	string str1, str2;
	cin >> str1 >> str2;
	for (int i = 0; i < str2.length(); i++) {
		if (str2[i] != '_') {
			if (isalpha(str2[i])) {
				int t;
				if (str2[i] >= 'a'&&str2[i] <= 'z') {
					t = str2[i] - 'a';
				}
				else {
					t = str2[i] - 'A';
				}
				alpha[t] = 1;
			}
			else {
				num[str2[i] - '0'] = 1;
			}
		}
		else {
			_ = 1;
		}
	}
	for (int i = 0; i < str1.length(); i++) {
		if (str1[i] != '_') {
			if (isalpha(str1[i])) {
				int t;
				if (str1[i] >= 'a'&&str1[i] <= 'z') {
					t = str1[i] - 'a';
				}
				else {
					t = str1[i] - 'A';
				}
				if (alpha[t] == 0) {
					printf("%c", ('A' + t));
					alpha[t] = 1;
				}
			}
			else {
				if (num[str1[i] - '0'] == 0) {
					//cout << i << endl;
					cout << str1[i] - '0';
				}
			}
		}
		else {
			if (_ == 0)cout << '_'; _ = 1;
		}
	}
	cout << endl;
	return 0;
}
//如果是小写字符则转换为大写字符,其他字符不变
toupper(char c);
//同样有相反效果的
tolower(char c);

我放弃之前的错误代码了,然后用了更简单的方法直接AC了:
在这里插入图片描述

学习到的技巧
//当在string中找不到时返回string::npos,例如:
string str;
str.find('a')==string::npos;

晴神的《算法笔记》里都有涉及到。

AC代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<queue>
#include<map>
#include<stack>
using namespace std;

int main() {
	string str1, str2, ans;
	cin >> str1 >> str2;
	for (int i = 0; i < str1.length(); i++) {
		if (str2.find(str1[i]) == string::npos&&ans.find(toupper(str1[i]))==string::npos) {
			ans += toupper(str1[i]);
		}
	}
	for (int i = 0; i < ans.length(); i++) {
		cout << ans[i];
	}
	cout << endl;
	return 0;
}

结语

总是有很多意料之外的事打断自己的刷题之路呢,80题遥遥无期,不过能做一道是一道嘛!每一次努力都会有收获!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值