天梯赛L1-020 帅到没朋友(map的使用)(模拟,数组非排序去重)

 

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有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

扫描二维码关注公众号,回复: 9665591 查看本文章

输出样例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

注意没朋友的人有两种,一种是朋友圈只有自己,一种是根本没出现,

此题使用了 map,还牵涉到数组的非排序去重(排序的话,就是直接压入set里或者是对数组进行unique操作)

非排序就需要用标记数组啦。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+10;
 4 const int inf=0x3f3f3f3f;
 5 
 6 string num[maxn];
 7 string ans[maxn];
 8 
 9 map<string,int> m,f;
10 
11 int main()
12 {
13     int n;
14     scanf("%d",&n);
15     for(int i=1;i<=n;i++)
16     {
17         int x;
18         scanf("%d",&x);
19         string cnt;
20         for(int j=1;j<=x;j++)
21         {
22             cin>>cnt;
23             if(x==1&&m[cnt]==0)//仅有1,或者根本没有的这种情况 
24              m[cnt]=inf;
25             else m[cnt]++;
26         }
27     }
28     int q;
29     scanf("%d",&q);
30     int k=0;
31     for(int i=1;i<=q;i++){//对查询数组进行去重 
32         string r;
33         cin>>r;
34         if(f[r]==0){
35             f[r]=1;//标记
36             num[++k]=r;
37         }
38     }
39     int l=0;
40     for(int i=1;i<=k;i++){
41         if(m[num[i]]==0||m[num[i]]==inf)
42             ans[++l]=num[i];//存到ans里 
43     }
44     if(l==0) cout<<"No one is handsome"<<endl;
45     else{
46         for(int i=1;i<l;i++)
47             cout<<ans[i]<<" ";
48         cout<<ans[l]<<endl;
49     }
50 }

猜你喜欢

转载自www.cnblogs.com/sweetlittlebaby/p/12443282.html