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

題目大意

根據(jù)IP地址找出用戶(hù)的大號(hào)和馬甲馒索。

思路

Person類(lèi)封裝id膀懈、ip、mj等信息和一些操作逞频,規(guī)定第一次出現(xiàn)的IP對(duì)應(yīng)的ID為大號(hào)退腥,其余相同IP的ID為馬甲任岸。一個(gè)大號(hào)有且只有一個(gè)馬甲。

代碼

// 1027.cpp£o 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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末狡刘,一起剝皮案震驚了整個(gè)濱河市享潜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌嗅蔬,老刑警劉巖剑按,帶你破解...
    沈念sama閱讀 216,843評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異澜术,居然都是意外死亡艺蝴,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)鸟废,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)猜敢,“玉大人,你說(shuō)我怎么就攤上這事盒延∷趵蓿” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,187評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵添寺,是天一觀的道長(zhǎng)胯盯。 經(jīng)常有香客問(wèn)我,道長(zhǎng)计露,這世上最難降的妖魔是什么博脑? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,264評(píng)論 1 292
  • 正文 為了忘掉前任憎乙,我火速辦了婚禮,結(jié)果婚禮上叉趣,老公的妹妹穿的比我還像新娘泞边。我一直安慰自己,他們只是感情好君账,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布繁堡。 她就那樣靜靜地躺著沈善,像睡著了一般乡数。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上闻牡,一...
    開(kāi)封第一講書(shū)人閱讀 51,231評(píng)論 1 299
  • 那天净赴,我揣著相機(jī)與錄音,去河邊找鬼罩润。 笑死玖翅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的割以。 我是一名探鬼主播金度,決...
    沈念sama閱讀 40,116評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼严沥!你這毒婦竟也來(lái)了猜极?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,945評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤消玄,失蹤者是張志新(化名)和其女友劉穎跟伏,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體翩瓜,經(jīng)...
    沈念sama閱讀 45,367評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡受扳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了兔跌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勘高。...
    茶點(diǎn)故事閱讀 39,754評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖坟桅,靈堂內(nèi)的尸體忽然破棺而出华望,到底是詐尸還是另有隱情,我是刑警寧澤桦卒,帶...
    沈念sama閱讀 35,458評(píng)論 5 344
  • 正文 年R本政府宣布立美,位于F島的核電站,受9級(jí)特大地震影響方灾,放射性物質(zhì)發(fā)生泄漏建蹄。R本人自食惡果不足惜碌更,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望洞慎。 院中可真熱鬧痛单,春花似錦、人聲如沸劲腿。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,692評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)焦人。三九已至挥吵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間花椭,已是汗流浹背忽匈。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,842評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留矿辽,地道東北人丹允。 一個(gè)月前我還...
    沈念sama閱讀 47,797評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像袋倔,于是被迫代替她去往敵國(guó)和親雕蔽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,850評(píng)論 0 0
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,471評(píng)論 5 6
  • 一場(chǎng)夏日的暴雨是不可避免的了宾娜。云批狐,在天上繃著臉,密不透風(fēng)碳默,太陽(yáng)在云背后東躲西藏贾陷。地面上的熱氣蒸著綠葉上的...
    心蓮朵朵00閱讀 222評(píng)論 0 0
  • (2.5) [“http://pan.baidu.com/share/link?shareid=299794683...
    感謝經(jīng)歷_31be閱讀 169評(píng)論 0 0
  • 親愛(ài)的,生日快樂(lè)嘱根! 你和兒子的生日一天之差髓废,記得生兒子那年,兒子的預(yù)產(chǎn)期是農(nóng)歷的九月十八该抒,誰(shuí)知這小...
    淺嫣然閱讀 622評(píng)論 8 1