目录
牛客_OR63删除公共字符_哈希
删除公共字符_牛客题霸_牛客网 (nowcoder.com)
题目解析
用哈希表记录一下字符串的字符信息即可。
C++代码1
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
int main()
{
string str1, str2;
getline(cin, str1);
getline(cin, str2);
unordered_map<char, int> hash; // str2的字母和个数
for(auto& e : str2)
{
hash[e]++;
}
string res;
for(auto& e : str1)
{
if(!hash.count(e))
res += e;
}
cout << res << endl;
return 0;
}
C++代码2
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, t;
getline(cin, s);
getline(cin, t);
bool hash[300] = { 0 };
for(char ch : t)
{
hash[ch] = true;
}
string ret;
for(auto& ch : s)
{
if(!hash[ch])
{
ret += ch;
}
}
cout << ret << endl;
return 0;
}
Java代码
import java.util.Scanner;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String t = br.readLine();
boolean[] hash = new boolean[300];
for(int i = 0; i < t.length(); i++)
{
hash[t.charAt(i)] = true;
}
for(int i = 0; i < s.length(); i++)
{
if(!hash[s.charAt(i)])
{
System.out.print(s.charAt(i));
}
}
}
}