Sicily 1027 Mj,nowhere to Hide

Source: Internet
Author: User

1027 MJ, Nowhere to Hide Constraints

Time limit:1 secs, Memory limit:32 MB

Description

On BBS, there are a familiar term called MJ (short for Majia), which means another BBS ID of one person besides his/her Mai N ID.
These days, a lot of acmers pour water on the acmicpc Board of Argo. Mr Guo is very angry on that and he wants to punish these guys. Acmers is all smart boys/girls, right?  They usually use their MJs and pouring water, so Mr. Guo can not tell all the IDs apart. Unfortunately, the IP can not is changed, i.e, the posts of the main ID and MJ of the same person has the same IP address, mea  Nwhile, the IP addresses of different is different. Assuming that all person have exactly one main ID and one MJ, by reading their posts on BBS, your then tell Mr. Guo whom EA Ch MJ belongs to.

Input

The first line of all test cases is a even integer n (0<=n<=20), the number of posts on BBS.
Then n lines follow, each of the line consists of the 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 are not greater than 12. Each BBS ID is appears only once with each test cases.
IP_Address is the IP address of this person. The IP address is formatted as "a.b.c.d", where A, B, C, D was integers ranging from 0 to 255.
It is sure that there be exactly 2 different BBS IDs with the same IP address. The first ID appears in the-the main ID while the other is the MJ of the.
Your program should is 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 is 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
8inkfish 192.168.29.24zhi 192.168.29.235magicpig 192.168.50.170pegasus 192.168.29.235iamcs 202.116.77.131finalBob 192.168.29.24tomek 202.116.77.131magicduck 192.168.50.1704mmmmmm 172.16.72.126kkkkkk 192.168.49.161llllll 192.168.49.161NNNNNN 172.16.72.1260
Sample Output
Tomek is the Majia of Iamcsfinalbob are the Majia of Inkfishmagicduck is the Majia of Magicpigpegasus of the Majia l Lllll is the Majia of kkkkkknnnnnn is the Majia of Mmmmmm
Problem Source

ZSUACM Team Member

The idea of this problem is also relatively simple, each given n sets of strings, each group including BBS account and IP address, each IP address corresponds to two different BBS accounts, the first given BBS account (MAIN_ID) is the BBS account (MJ_ID) is given after the Majia, and then according to Main_ The dictionary order of the IDs outputs all corresponding sequences.

First, establish a <string, string> type Map association m storage Bbs-ip correspondence relationship, build the structure array s to store all the correspondence between BBS;

And then traverse all BBS-IP correspondence, when an IP first appears its m-associative string is empty, at this time to give its corresponding BBS value; When an IP second appears that its associated string is not empty, at this time the IP has been associated with the BBS is main_id, just traversed to the BBS value is MJ _ID, it is written to the struct array, the array jumps to the next storage unit, and the counter P calculates the number of the resulting structure array s;

Finally, the custom CMP to the structure of the array s to sort, the keyword is main_id, sorted by the sort, according to the requirements of the output can be, it is important to note that each test sample finally have a blank line, do not miss out.

The code is as follows:

#include <iostream> #include <map> #include <algorithm>using namespace std;struct node{string main_id ; string mj_id;} S[106];bool CMP (const node &A,CONST node &b) {return a.main_id<b.main_id;} int main () {int n;string bbs,ip;while (cin>>n) {if (n==0) break;map<string,string> m;int p=0;for (int i=0;i <n;i++) {cin>>bbs>>ip;if (m[ip]== "") M[ip]=bbs;else {s[p].main_id=m[ip];s[p].mj_id=bbs;p=p+1;}} Sort (s,s+p,cmp); for (int. i=0;i<p;i++) {cout<<s[i].mj_id<< "is the Majia of" <<s[i].main_id< <endl;} Cout<<endl;} return 0;}

  

Sicily 1027 Mj,nowhere to Hide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.