The title description
"Single Dog" is a Chinese nickname for single people.
This question asks you to find out the single guests from a large party of tens of thousands of people, so that you can give special care.
Input format The
first line of input gives a positive integer N, which is the logarithm of the known couple/partner; the
subsequent N lines, each line gives a couple/partner-for convenience, each person corresponds to an ID number, which is 5 digits (from 00000 to 99999), the IDs are separated by spaces;
after that, a positive integer M is given, which is the total number of people participating in the party; the
next line gives the IDs of the M guests, separated by spaces.
The title guarantees that no one has bigamy or two boats.
Output format
First, the first line outputs the total number of guests who placed orders;
then the second line lists the guests who placed orders in ascending order of ID. The IDs are separated by a space, and there must be no extra spaces at the beginning and end of the line.
Input sample
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
Output sample
5
10000 23333 44444 55555 88888
Data range
N ≤ 50 000
M ≤ 10 000
Problem solution
STL:
解题步骤
:
- First with
map
each mutually couple mapping; - And then
set
recorded to the party of people; - Finally enumerate all
set
the elements, if their spouse is notset
, then added toans
go;
#include <iostream>
#include <unordered_map>
#include <vector>
#include <set>
using namespace std;
int n, m;
vector<string> ans;
unordered_map<string, string> H;
set<string> S;
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
string a, b;
cin >> a >> b;
H[a] = b, H[b] = a;
}
cin >> m;
for (int i = 1; i <= m; i ++)
{
string s;
cin >> s;
S.insert(s);
}
for (auto &x : S)
if(!S.count(H[x])) ans.push_back(x);
cout << ans.size() << endl;
bool flag = true;
for (int i = 0; i < ans.size(); i ++)
if(flag) cout << ans[i], flag = false;
else cout << ' ' << ans[i];
return 0;
}
Misunderstanding: Test point 2 segment error, but no error can be found
#include <iostream>
#include <map>
#include <set>
using namespace std;
int n, m;
map<string, string> H;
set<string> S;
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
string a, b;
cin >> a >> b;
H[a] = b, H[b] = a;
}
cin >> m;
for (int i = 1; i <= m; i ++)
{
string s;
cin >> s;
S.insert(s);
}
for (auto &x : S)
if(S.count(H[x]))
{
S.erase(S.find(x));
S.erase(S.find(H[x]));
if(S.empty()) break;
}
cout << S.size() << endl;
bool flag = true;
for (auto &x : S)
if(flag) cout << x, flag = false;
else cout << ' ' << x;
return 0;
}