PAT (Advanced Level) Practise 1121. Damn Single (25)

来源:互联网 发布:c语言入门程序设计 编辑:程序博客网 时间:2024/06/06 06:49

1121.Damn Single
时间限制: 400 ms
内存限制: 65536 kB
代码长度限制: 16000 B
判题程序:Standard


  “Damn Single (单身狗)” is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.

Input
  Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID’s which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID’s of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.

Output
  First print in a line the total number of lonely guests. Then in the next line, print their ID’s in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.

Examples

Input 3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333 Output 5
10000 23333 44444 55555 88888

 
Notes
  

作者
  CHEN, Yue

  用map建立couple中两人id的映射,用set保存party中guest的id。遍历set中的每个id,如果某个id在map中存在映射map[id],且map[id]也属于set,那个这个id所代表的客人就不是lonely guest。set的遍历是有序的,只需要将符合lonely guest的id依次存在一个数组中即可。
  注意测试点中存在M=0的测试用例。如果用下面这种常用的方式进行输出,会发生越界。(M=0时,cnt为0)

for (int i = 0; i < cnt - 1; i++)
cout << ans[i] << ” “;
cout << ans[cnt - 1] << “\n”;

#include <iostream>#include <algorithm>#include <map>#include <vector>#include <functional>#include <string>#include <cstring>#include <queue>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <sstream>#include <iomanip>using namespace std;#define IOS ios_base::sync_with_stdio(false)#define TIE std::cin.tie(0)#define MIN2(a,b) (a<b?a:b)#define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))#define MAX2(a,b) (a>b?a:b)#define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))typedef long long LL;typedef unsigned long long ULL;const int INF = 0x3f3f3f3f;const double PI = 4.0*atan(1.0);const double eps = 1e-6;map<string,string> mp;set<string> sp;int n, m, cnt;string s1, s2, ans[10005];int main(){    IOS;    cin >> n;    for (int i = 0; i < n; i++){        cin >> s1 >> s2;        mp[s1] = s2;        mp[s2] = s1;    }    cin >> m;    for (int i = 0; i < m; i++){        cin >> s1;        sp.insert(s1);    }    cnt = 0;    set<string>::iterator it;    for (it = sp.begin(); it != sp.end(); it++){        if (!(mp.count(*it) && sp.count(mp[*it])))            ans[cnt++] = *it;    }    cout << cnt << "\n";    for (int i = 0; i < cnt; i++){        cout << (i == 0 ? "" : " ") << ans[i];        if (i == cnt - 1) cout << "\n";    }    //system("pause");}//system("pause");
0 0
原创粉丝点击