Sample Input
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
Sample Output
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
这道题基本上就是寻找同一个ip下的两个名字,并且要按照原名的字典序输出。考虑到ip是唯一的,而名字不同,且要按照字典序。可以用两个map解决,一个map用来存储不重复的唯一的ip和对应的原名字,边输入边存储到map,如果输入的ip在map里不存在,则把这个元组tuple存到map中。如果输入的ip在map里存在,则说明这个元组存在原名,就在此时把这个假名连通真名一起存储到第二个map,以原名字作为key,这样就实现了以原名字字典序排序(map自动以key的字典序排序)代码见下:
// Problem#: 1027
// Submission#: 2936513
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int T;
while (cin >> T && T != 0)
{
string ip, name;
map<string, string> m;
map<string, string>::iterator it;
map<string, string> res;
while (T--)
{
cin >> name >> ip;
it = m.find(ip);
if (it == m.end()) // 不在里面,即不重复,则加入map
{
m.insert(make_pair(ip, name));
}
else // 在里面,即重复,则加入输出队列
{
res.insert(make_pair(it->second, name + " is the MaJia of " + it->second));
}
}
for (it = res.begin(); it != res.end(); it++)
cout << it->second << endl;
cout << endl;
}
}