方法1
#include <iostream>
#include <string>
#include <map>
#include <cctype>
using namespace std;
int main(){
string s1,s2;
cin>>s1>>s2;
map<char,int> mp;
for(int i=0;i<s1.length();i++){//将s1中的小写字母转化为大写字母
if(islower(s1[i])){
s1[i]=toupper(s1[i]);
}
}
for(int i=0;i<s2.length();i++){//将s2中的小写字母转化为大写字母
if(islower(s2[i])){
s2[i]=toupper(s2[i]);
}
}
for(int i=0;i<s1.length();i++){//枚举s1中的所有字符,在s2中查找是否存在,不存在就输出
if(s2.find(s1[i])==-1){
if(mp[s1[i]]) continue;//如果满足条件当前字符已经输出,则不输出
else{
mp[s1[i]]++;
cout<<s1[i];
}
}
}
return 0;
}
方法2:哈希表
#include <iostream>
#include <map>
#include <cctype>
using namespace std;
int main(){
string s1,s2;
map<char,int> hash1,hash2;//hash1为对s2建立的哈希表,hash2为已经输出的坏键建立的哈希表
cin>>s1>>s2;
for(int i=0;i<s1.size();i++){//不妨把小写字母全转为大写
if(islower(s1[i])){
s1[i]=toupper(s1[i]);
}
}
for(int i=0;i<s2.size();i++){//不妨把小写字母全转为大写
if(islower(s2[i])){
s2[i]=toupper(s2[i]);
}
}
for(auto c:s2){
hash1[c]++;
}
for(auto c:s1){
if(hash1[c]==0 && hash2[c]==0){//对于未出现在s2中的字符且之前没有输出
cout<<c;
hash2[c]++;
}
}
return 0;
}