最近某地连续发生了多起盗窃案件。根据监控和路人提供的线索得知。这是一个犯罪团伙。并且还知道这个犯罪团伙中每一个人的身高、体重、年龄。警察想知道这个犯罪团伙中的每个人是不是本市的(如果本市有这个特征的人就是本市的)。但是本市人口太多。不能一个个排查。警察急需这条信息来缩小范围,所以警察特地找到聪明的你来帮忙解决这个棘手的问题。
输入格式
第一行将会输入两个数字n(1<=n<=2*105)和m(1<=m<=104)。n代表本市的人口数目。m代表犯罪团伙的数量。
后面n行每行有3个数字代表本市每个人的身高、体重、年龄、。然后会有m行每行有3个数字代表犯罪团伙每个人的身高、体重、年龄。
输出格式
输出m行,每行输出一个YES或者NO,YES代表是本市的,NO代表不是本市的。
样例输入:
3 2
166 50 30
178 60 23
132 40 15
167 50 30
178 60 23
样例输出:
NO
YES
分析:首先把身高体重打包成结构体,然后存到集合中,接着在集合利用count()判断集合中有没有即可
注意:
1.由于有三组数据且是结构体,所以我们要重载运算符,判断三种情况
2.在存入集合前,我们可以在结构体声明是就写一个函数将输入直接存入结构体中,这样方便对集合进行操作,我把这个称为打包,然后直接调用结构体名称存入集合中。
#include<bits/stdc++.h>
using namespace std;
struct people{//定义结构体
int height ;
int weight;
int age;
people(int h_,int w_,int age_)//打包
{
height=h_;
weight=w_;
age=age_;
}
bool operator<(const people &rhs)const//重载运算符
{
if(height!=rhs.height)
{
return height<rhs.height;
}
if(weight!=rhs.weight)
{
return weight<rhs.weight;
}
else
return age<rhs.age;
}
};
set<people>s; //声明集合
int main()
{
int n,m,h1,w1,age1;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>h1>>w1>>age1;
s.insert(people(h1,w1,age1));//将打包好的结构体放到集合中
}
for(int i=0;i<m;i++)
{
cin>>h1>>w1>>age1;
if(s.count(people(h1,w1,age1)))//判断结构体是否存在
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
运行结果:
第二种结构体使用方式:
#include<iostream>
#include<set>
#include<cstdio>
#include<string>
using namespace std;
struct pople{
int h;
int w;
string age;
bool operator<(const pople & rhs)const
{
if(h!=rhs.h){
return h<rhs.h;
}
if(w!=rhs.w){
return w<rhs.w;
}else{
return age<rhs.age;
}
}
};
set<pople>s;
int main()
{
string age;
int n,h,w;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>h>>w>>age;
s.insert({h,w,age});//直接打包输入
}
for(int i=0;i<m;i++)
{
cin>>h>>w>>age;
if(s.count({h,w,age}))//打包判断 ,记得大括号
printf("YES");
else
printf("NO");
}
set<pople>::iterator it;
for(it=s.begin();it!=s.end();it++)
cout<<it->h<<" "<<it->w<<" "<<it->age<<" "<<endl;
return 0;
}