sicily_1027 MJ, Nowhere to Hide

题目

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为马甲。一个大号有且只有一个马甲。

代码

// 1027.cpp£º a developed version
// I use class to encapsulate the structure and methods it use.
// Copyright (c) 2014 Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
#include <iostream>
#include <string>
#include <algorithm>
#include <list>

using std::cin;
using std::cout;
using std::endl;
using std::string;

class Person {
public:
  Person(string ID, string IP, string MJ = "") {
    main_ID_ = ID;
    IP_ = IP;
    MJ_ = MJ;
  }

  string getMainID() { return main_ID_; }
  void setMainID(string ID) { main_ID_ = ID; }
  string getIP() { return IP_; }
  void setIP(string IP) { IP_ = IP; }
  string getMJ() { return MJ_; }
  void setMJ(string ID) { MJ_ = ID; }

  // redim the operator of operator<
  bool operator<(Person B) {
    return getMainID() < B.getMainID();
  }

  friend std::ostream& operator<<(std::ostream& output, Person one) {
    output << one.MJ_ << " is the MaJia of " << one.main_ID_ << endl;
    return output;
  }

private:
  string main_ID_;
  string IP_;
  string MJ_;
};

int main() {
  int n;
  while (cin >> n && n && n % 2 == 0) {  // if the input is an odd number.
    std::list<Person> list;
    std::list<Person>::iterator it;
    string ID;
    string IP;

    for (int i = 0; i < n; i++) {
      cin >> ID >> IP;
      for (it = list.begin(); it != list.end(); it++) {
        if (it->getIP() == IP) {
          it->setMJ(ID);
          break;
        }
      }
      if (it == list.end()) {
        list.push_back(Person(ID, IP));
      }
    }

    // here is an algorithm simplified.
    list.sort();

    for (it = list.begin(); it != list.end(); it++) {
      cout << *it;
    }
  }
  return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,936评论 0 0
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,551评论 5 6
  • 一场夏日的暴雨是不可避免的了。云,在天上绷着脸,密不透风,太阳在云背后东躲西藏。地面上的热气蒸着绿叶上的...
    心莲朵朵00阅读 238评论 0 0
  • (2.5) [“http://pan.baidu.com/share/link?shareid=299794683...
    感谢经历_31be阅读 267评论 0 0
  • 亲爱的,生日快乐! 你和儿子的生日一天之差,记得生儿子那年,儿子的预产期是农历的九月十八,谁知这小...
    浅嫣然阅读 641评论 8 1