SOJ 1027 Mj,nowhere to Hide

Source: Internet
Author: User

Main topic: input n rows (ID, IP) pairs, the same IP, the first IP corresponding ID is main_id, the second IP corresponding ID is mj_id, output N/2 (main_id, mj_id) pair, the output format is the mj_id of main_id, output is output by main_id dictionary sequence. When n is 0 o'clock, the input ends.

Solution thinking: This is a mapping problem, using a data structure map is very appropriate. Each read-in (ID, IP) pair, check whether the map has a key value for the IP key, if not present, then insert (ID, IP) pair, if present, it is stored in the result mapping table.

Finally, traverse the result mapping table to output the answer.

The code is as follows:

#include <iostream>#include<map>#include<string>using namespacestd;intMain () {intN;  while(Cin >>N, N) {Map<string,string>m; Map<string,string>ans; stringID, IP;  for(inti =0; I < n; i++) {cin>> ID >>IP; if(M.find (IP) = =M.end ()) {M[ip]=ID; } Else{Ans[m[ip]]=ID; }} Map<string,string>::iterator iter =Ans.begin ();  while(ITER! =Ans.end ()) {cout<< Iter->second <<"is the Majia of"<< Iter->first <<Endl; ITER++; } cout<<Endl; }    return 0;}

SOJ 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.