题目链接->link
思路
- 思路和B1029类似,但是有几个坑。
- 首先用hashmap数组存储不能输出的字符,设为false,初始化用memset(),初值为true,不能像B1029初始化为bool hashmap[]=false。
- 注意坏掉的字符统一用大写输入,那么要将其转换为小写,将小写的hashmap[]设为false。
- 当要输出大写字母时,仅当’+'号和其小写字母对于hashmap[]值为true时,才能输出。
- 测试点3是键盘不坏,即输入第一行为空时,要用gets()读取,因为scanf()读到空格就结束;但是pat的gcc不支持gets(),所以可以用fgets(str1,maxn,stdin)或者getline()读入。
代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=100005;
char str1[maxn],str2[maxn];
bool hashmap[128];
int main(){
memset(hashmap,true,sizeof(hashmap));//存储字符是否可以输出
fgets(str1, maxn, stdin);
fgets(str2, maxn, stdin);
int i;
int len1=strlen(str1);
for(i=0;i<len1;i++){//枚举不能输出的所有字符
char c1=str1[i];
if(c1>='A'&&c1<='Z')c1+=32;//大写转小写
hashmap[c1]=false;//更新不能输出的字符
}
int len2=strlen(str2);
for(i=0;i<len2;i++){//枚举应该输入的字符串
char c2=str2[i];
if(c2>='A'&&c2<='Z'){//如果要输入大写
c2+=32;//大写转小写
if(hashmap['+']==true&&hashmap[c2]==true){//如果+号和小写字母均为true才能输出
printf("%c",c2-32);//小写还原成大写
}
}
else if(hashmap[c2]==true){
printf("%c",c2);//非大写字母,只要键位为true均可输出
}
}
printf("\n");//没有字符可以输出,则输出空行
return 0;
}