哈希表,使用set自动排序
C++ 代码
#include <iostream>
#include <set>
#include <unordered_map>
using namespace std;
int m, n;
int main(){
unordered_map<string, string> couple;
set<string> people, people1;
cin >> m;
for(int i = 0; i < m; ++ i) {
string m, f;
cin >> m >> f;
couple[m] = f;
couple[f] = m;
}
cin >> n;
for(int i = 0; i < n; ++ i){
string s;
cin >> s;
people.insert(s);
}
people1 = people;
for(auto& x : people1){
if(couple.count(x) == 1 && people1.count(couple[x]) == 1){
people.erase(x);
people.erase(couple[x]);
}
}
cout << people.size() << endl;
for(auto& x : people)
cout << x << ' ';
return 0;
}