查找共同好友
时间:2010-12-14 来源:zhaos
所有记录
要查找多少人的共同好友
这些人的姓名6
张潮 刘磊 902419 22
王磊 赵磊 902429 22
张潮 张平一 902436 21
李平一 白上由 902422 22
王浩 王洗星 902420 20
王浩 张平一 902436 21
2
张潮 王浩
#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> using namespace std; class Person { public: string name; int num; int age; Person():name(""),num(0),age(0){ } friend bool operator==(const Person &a, const Person &b) { return a.name== b.name && a.num == b.num && a.age == b.age; } friend ostream& operator<<(ostream& out,const Person &obj) { out<<obj.name<<" "<<obj.num<<" "<<obj.age; return out; } friend istream& operator>>(istream& in, Person &record) { in>>record.name>>record.num>>record.age; return in; } }; typedef multimap<string ,Person*>::iterator ssit; int main() { multimap<string, Person*> Record; string Name; Person Friend; int a=0,b; cin>>a; Person *r = new Person[a]; for(int i=0;i<a;i++) { cin>>Name>>Friend; r[i] = Friend; Record.insert(make_pair(Name,&r[i])); } cin>>b; vector<string> NamesToFind; vector<Person> Names_Finded; for(int i=0;i<b;i++) { cin>>Name; if( count(NamesToFind.begin(),NamesToFind.end(),Name)==0 ) NamesToFind.insert(NamesToFind.begin(),Name); } vector<string>::iterator it1=NamesToFind.begin(); vector<string>::iterator it2=NamesToFind.end(); for(;it1!=it2;++it1) { ssit beg = Record.lower_bound(*it1); ssit end = Record.upper_bound(*it1); while(beg!=end) { Names_Finded.push_back(*(beg->second)); ++beg; } } vector<string> publicfriends; for(unsigned int i=0;i<Names_Finded.size();i++) { if( count(Names_Finded.begin(),Names_Finded.end(),Names_Finded[i]) ==b && count(publicfriends.begin(),publicfriends.end(),Names_Finded[i].name) ==0 ) { publicfriends.push_back(Names_Finded[i].name); cout<<Names_Finded[i]<<endl; } } if(publicfriends.size()==0) cout<<"NO"; return 0; }
相关阅读 更多 +