On BBS, there is a familiar term called MJ (short for MaJia), which means another BBS ID of one person besides his/her main ID.
These days, a lot of ACMers pour water on the ACMICPC Board of argo. Mr. Guo is very angry about that and he wants to punish these guys. ACMers are all smart boys/girls, right? They usually use their MJs while pouring water, so Mr. Guo can not tell all the IDs apart. Unfortunately, the IP can not be changed, i.e, the posts of main ID and MJ of the same person has the same IP address, meanwhile, the IP addresses of different person is different. Assuming that each person has exactly one main ID and one MJ, by reading their posts on BBS, you then tell Mr. Guo whom each MJ belongs to.
The first line of each test cases is an even integer n (0<=n<=20), the number of posts on BBS.
Then n lines follow, each line consists of two strings:
BBS_ID IP_Address
BBS_ID means the ID who posts this post. BBS_ID is a string contains only lower case alphabetical characters and its length is not greater than 12. Each BBS ID appears only once in each test cases.
IP_Address is the IP address of that person. The IP address is formatted as “A.B.C.D”, where A, B, C, D are integers ranging from 0 to 255.
It is sure that there are exactly 2 different BBS IDs with the same IP address. The first ID appears in the input is the main ID while the other is the MJ of that person.
Your program should be terminated by n = 0.
For each test case, output n/2 lines of the following format: “MJ_ID is the MaJia of main_ID”
They should be displayed in the lexicographical order of the main_ID.
Print a blank line after each test cases.
See the sample output for more details.
8 inkfish 192.168.29.24 zhi 192.168.29.235 magicpig 192.168.50.170 pegasus 192.168.29.235 iamcs 202.116.77.131 finalBob 192.168.29.24 tomek 202.116.77.131 magicduck 192.168.50.170 4 mmmmmm 172.16.72.126 kkkkkk 192.168.49.161 llllll 192.168.49.161 nnnnnn 172.16.72.126 0
tomek is the MaJia of iamcs finalBob is the MaJia of inkfish magicduck is the MaJia of magicpig pegasus is the MaJia of zhi llllll is the MaJia of kkkkkk nnnnnn is the MaJia of mmmmmm
Problem Source: ZSUACM Team Member
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct BBS_ID //定义一个ID结构体,包含ID的名称和ip地址
{
string ID_name;
string ID_ip;
};
bool myComp(const BBS_ID &b1,const BBS_ID &b2) //定义一个比较方法,对main_ID比大小
{
if((b1.ID_name).compare(b2.ID_name)<0) //在下面的程序中当找到配对后main_ID存放在ID_name中
return b1.ID_name<b2.ID_name; //马甲ID存放在ID_ip中
else return b1.ID_name<b2.ID_name;
}
int main()
{
int iCount;
while(cin>>iCount){ //记录有多少对数据
if(iCount == 0) return 0;
string strA,strB;
BBS_ID bbs;
vector<BBS_ID> vID; //定义结构体类型的向量
for(int i=0;i<iCount;i++) //读入一组ID,放入结构体
{
cin>>strA>>strB;
bbs.ID_name=strA;
bbs.ID_ip=strB;
vID.push_back(bbs); //存入向量
}
vector<BBS_ID>::iterator it,itt;
vector<BBS_ID> vFinal; //将找出ip相同的一组向量的名称存入结构体类型向量
for(it=vID.begin();it!=vID.end();it++) //遍历向量找出ip相同的ID
{
for(itt=it+1;itt!=vID.end();itt++)
{
if((*it).ID_ip==(*itt).ID_ip)
if(((*it).ID_ip).compare((*itt).ID_ip)>0) //ID长度小的为main_ID
{
bbs.ID_name=(*itt).ID_name; //main_ID存放在ID_name中
bbs.ID_ip=(*it).ID_name; //马甲ID存放在ID_ip中
vFinal.push_back(bbs);
}
else
{
bbs.ID_name=(*it).ID_name;
bbs.ID_ip=(*itt).ID_name;
vFinal.push_back(bbs);
}
}
}
sort(vFinal.begin(),vFinal.end(),myComp); //按要求排序
vector<BBS_ID>::iterator itf;
for(itf=vFinal.begin();itf!=vFinal.end();itf++)
cout<<(*itf).ID_ip<<" is the MaJia of "<<(*itf).ID_name<<endl;
cout<<endl;
}
return 0;
}