總結
如果要在set或map 存結構體 就一定要重定義 "<"
對於結構體數組進行排序,cmp函數是必須的
格式化輸出補零用%04d ,保留4位小數用%.4f
當有例子過不去的時候 一定要仔細讀題 把邏輯理順
題目看不懂的時候一定要仔細分析測試用例
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
struct stu{
int id;
bool gender;
bool operator <(const stu &o) const{
return id<o.id;
}
};
struct p{
int id1;
int id2;
};
int s2int(string s){
int t=0;
for(int i=0;s[i];++i){
t=t*10+s[i]-'0';
}
return t;
}
map<stu,vector<stu> > graph;
void addnode(stu st1,stu st2){
if(graph.find(st1)!=graph.end()){
graph[st1].push_back(st2);
}else{
vector<stu> temp;
temp.push_back(st2);
graph[st1]=temp;
}
}
stu str2stu(string str1){
stu s1;
if(str1[0]=='-'){
s1.gender=false;
str1=str1.substr(1,4);
}else{
s1.gender=true;
}
s1.id=s2int(str1);
return s1;
}
vector<p> suitpair(stu s1,stu s2){
vector<p> v;
for(auto i:graph[s1]){
if(i.id==s2.id||i.gender!=s1.gender) continue;
for(auto j:graph[i]){//
if(j.id==s1.id||j.gender!=s2.gender) continue;
for(auto k:graph[j]){
if(k.id==s2.id){
v.push_back({i.id,j.id});
}
}
}
}
return v;
}
bool cmp(p x,p y){
if(x.id1!=y.id1) return x.id1<y.id1;
return x.id2<y.id2;
}
int main(){
int n,m,k;
cin>>n>>m;
string str1,str2;
char cstr1[6],cstr2[6];
while(m--){
scanf("%s%s",cstr1,cstr2);//str1>>str2;
str1=string(cstr1);
str2=string(cstr2);
stu st1=str2stu(str1);
stu st2=str2stu(str2);
addnode(st1,st2);
addnode(st2,st1);
}
cin>>k;
while(k--){
scanf("%s%s",cstr1,cstr2);//str1>>str2;
str1=string(cstr1);
str2=string(cstr2);
vector<p> ans=suitpair(str2stu(str1),str2stu(str2));
sort(ans.begin(),ans.end(),cmp);
cout<<ans.size()<<endl;
for(auto i:ans){
printf("%04d %04d\n",i.id1,i.id2);//i.id1<<" "<<i.id2<<endl;
}
}
return 0;
}