https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672
1033 旧键盘打字(20 分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?
输入格式:
输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 105 个字符的串。可用的字符包括字母 [a
-z
, A
-Z
]、数字 0
-9
、以及下划线 _
(代表空格)、,
、.
、-
、+
(代表上档键)。题目保证第 2 行输入的文字串非空。
注意:如果上档键坏掉了,那么大写的英文字母无法被打出。
输出格式:
在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。
输入样例:
7+IE.
7_This_is_a_test.
输出样例:
_hs_s_a_tst
思路及关键点:
1、不管有没有+,只要出现了大写字母,那么大写字符和其对应的小写字母都应被加入到哈希表中,在最后输出中都应该被过滤。
#include <iostream>
#include <cstring>
#include <string>
#include <map>
using namespace std;
const int nmax=1e5+10;
char s1[nmax];
char s2[nmax];
int hashtable[nmax];
int main(int argc, char** argv)
{
while(cin.getline(s1,nmax)&&cin.getline(s2,nmax)){
memset(hashtable,0,sizeof(hashtable));
int len1=strlen(s1);
int len2=strlen(s2);
int flag=1;//表示是否能打出大写字母
for(int i=0;i<len1;i++){
if(s1[i]=='+'){
flag=0;
}
if(s1[i]>='A'&&s1[i]<='Z'){
char c=s1[i];
s1[i]=tolower(s1[i]);
hashtable[s1[i]]=1;
hashtable[c]=1;
}else{
hashtable[s1[i]]=1;
}
}
//cout<<s1<<endl; //测试小写转换后的s1
for(int i=0;i<len2;i++){
if(flag==1){
if(hashtable[s2[i]]==0){
cout<<s2[i];
}
}
else{//不能打出
if(s2[i]<'A'||s2[i]>'Z'){
if(hashtable[s2[i]]==0){
cout<<s2[i];
}
}
}
}
}
return 0;
}
/*
AC
aAbBcCdD
bBdD
*/