L1-020 帅到没朋友 (20 分)
当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。
输入格式:
输入第一行给出一个正整数N(≤),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(≤),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(≤),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。
注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。
输出格式:
按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出No one is handsome。
注意:同一个人可以被查询多次,但只输出一次。
输入样例1:
3 3 11111 22222 55555 2 33333 44444 4 55555 66666 99999 77777 8 55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3 3 11111 22222 55555 2 33333 44444 4 55555 66666 99999 77777 4 55555 44444 22222 11111
输出样例2:
No one is handsome
代码:
1 //1
2 #include<bits/stdc++.h>
3 using namespace std;
4 const int maxn=1e5+10;
5 const int inf=0x3f3f3f3f;
6
7 string num[maxn];
8 string ans[maxn];
9
10 map<string,int>mp,w;
11
12 int main()
13 {
14 int n;
15 scanf("%d",&n);
16 for(int i=1;i<=n;i++){
17 int x;
18 scanf("%d",&x);
19 string cnt;
20 for(int j=1;j<=x;j++){
21 cin>>cnt;
22 if(x==1&&mp[cnt]==0) mp[cnt]=inf;
23 else mp[cnt]++;
24 }
25 }
26 int q;
27 scanf("%d",&q);
28 int id=0;
29 for(int i=1;i<=q;i++){
30 string ret;
31 cin>>ret;
32 if(w[ret]==0){
33 w[ret]=1;
34 num[++id]=ret;
35 }
36 }
37 int l=0;
38 for(int i=1;i<=id;i++){
39 if(mp[num[i]]==0||mp[num[i]]==inf)
40 ans[++l]=num[i];
41 }
42 if(l==0) cout<<"No one is handsome"<<endl;
43 else{
44 for(int i=1;i<l;i++)
45 cout<<ans[i]<<" ";
46 cout<<ans[l]<<endl;
47 }
48 }
来源:https://www.cnblogs.com/ZERO-/p/10623814.html