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