版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37454852/article/details/86479849
1033 旧键盘打字 (20 分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?
输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10
5
个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。
注意:如果上档键坏掉了,那么大写的英文字母无法被打出。
输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。
输入样例:
7+IE.
7_This_is_a_test.
输出样例:
_hs_s_a_tst
using namespace std;
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
int Key[50] = {0};
int mapping(char ch)//把字母映射到某个按键上
{
if(ch >= 'a' && ch <= 'z') return ch-'a';//A~Z在0~25
if(ch >= 'A' && ch <= 'Z') return ch-'A';
if(ch >= '0' && ch <= '9') return ch-'0' + 26;//0~9在26~35
if(ch == '_' ) return 36;//_在36
if(ch == ',' ) return 37;//,在37
if(ch == '.' ) return 38;//.在38
if(ch == '-' ) return 39;//-在39
return 40;//+在40
}
string str1;
char str2[100010] = {0};
int main()
{
getline(cin, str1);//第一行可能为空,这里用了string类,用getline进行输入;有个测试用例就是这个坑
scanf("%s", str2);//C程序员伤不起啊
int len1 = str1.length();
int len2 = strlen(str2);
int x = 0;
for(int i=0; i<len1; i++)
{
x = mapping(str1[i]);
Key[x] = 1;//该键坏掉则置一
}
for(int i=0; i<len2; i++)
{
if(str2[i] >= 'A' && str2[i] <= 'Z' && Key[40]) continue;//+坏掉,所有大写字母不输出
x = mapping(str2[i]);//定位是哪个键
if(!Key[x]) printf("%c", str2[i]);//若没有坏则能正常输出
}
return 0;
}