[关闭]
@DingCao-HJJ 2015-09-29T16:58:26.000000Z 字数 3178 阅读 1161

sicily_1027 MJ, Nowhere to Hide

sicily


题目

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

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.

Input

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.

Output

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.

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地址找出用户的大号和马甲。

思路

Person类封装id、ip、mj等信息和一些操作,规定第一次出现的IP对应的ID为大号,其余相同IP的ID为马甲。一个大号有且只有一个马甲。

代码

  1. // 1027.cpp£º a developed version
  2. // I use class to encapsulate the structure and methods it use.
  3. // Copyright (c) 2014 Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
  4. #include <iostream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <list>
  8. using std::cin;
  9. using std::cout;
  10. using std::endl;
  11. using std::string;
  12. class Person {
  13. public:
  14. Person(string ID, string IP, string MJ = "") {
  15. main_ID_ = ID;
  16. IP_ = IP;
  17. MJ_ = MJ;
  18. }
  19. string getMainID() { return main_ID_; }
  20. void setMainID(string ID) { main_ID_ = ID; }
  21. string getIP() { return IP_; }
  22. void setIP(string IP) { IP_ = IP; }
  23. string getMJ() { return MJ_; }
  24. void setMJ(string ID) { MJ_ = ID; }
  25. // redim the operator of operator<
  26. bool operator<(Person B) {
  27. return getMainID() < B.getMainID();
  28. }
  29. friend std::ostream& operator<<(std::ostream& output, Person one) {
  30. output << one.MJ_ << " is the MaJia of " << one.main_ID_ << endl;
  31. return output;
  32. }
  33. private:
  34. string main_ID_;
  35. string IP_;
  36. string MJ_;
  37. };
  38. int main() {
  39. int n;
  40. while (cin >> n && n && n % 2 == 0) { // if the input is an odd number.
  41. std::list<Person> list;
  42. std::list<Person>::iterator it;
  43. string ID;
  44. string IP;
  45. for (int i = 0; i < n; i++) {
  46. cin >> ID >> IP;
  47. for (it = list.begin(); it != list.end(); it++) {
  48. if (it->getIP() == IP) {
  49. it->setMJ(ID);
  50. break;
  51. }
  52. }
  53. if (it == list.end()) {
  54. list.push_back(Person(ID, IP));
  55. }
  56. }
  57. // here is an algorithm simplified.
  58. list.sort();
  59. for (it = list.begin(); it != list.end(); it++) {
  60. cout << *it;
  61. }
  62. }
  63. return 0;
  64. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注