1027. MJ, Nowhere to Hide题解

原题:

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

大概题意:给出偶数个集合<BBS_ID,IP_ADDRESS>,其中规定同一个Ip地址都有一个主BBS_ID,另一个则为灌水ID,根据Ip地址找出这组关系,同一个Ip地址出现的第一个BBS_ID为主要ID,第二次出现的则为灌水ID,得到组合后,根据字典序进行排序按照格式输出即可。

具体代码如下:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//储存结果集合的结构
struct answerSet{
	string maja;
	string main_id;
};
//快排函数所需的比较函数,根据主Id进行排序
int compare(const void* a, const void* b){
	if( (*(answerSet*)a).main_id > (*(answerSet*)b).main_id )
		return 1;
	else 
	    return 0;
}
int main(){
	int n;
	while(cin>>n&&n!=0){
		string hold_ID_IP[25][2];
		//动态分配储存空间
		int* assist = new int[n];
		answerSet* answer = new answerSet[n/2];
		for(int i = 0; i < n; i++){
			cin>>hold_ID_IP[i][0];
			cin>>hold_ID_IP[i][1];
			assist[i] = 0;
		}
		//内嵌循环遍历相同Ip地址的ID,并将其存下
		for(int i = 0,k = 0; i < n; i++){
			if(assist[i] == 0)
				for(int j = i+1; j < n; j++){
					if(hold_ID_IP[i][1] == hold_ID_IP[j][1]){
						answer[k].main_id = hold_ID_IP[i][0];
						answer[k].maja = hold_ID_IP[j][0];
						k++;
						assist[i] = 1;
						assist[j] = 1;
					}
				}
		}
		//按照main_id进行排序
		qsort(answer,n/2,sizeof(answerSet),compare);
		//格式化输出
		for(int i = 0; i < n/2; i++){
			cout<<answer[i].maja<<" is the MaJia of "<<answer[i].main_id<<endl;
		}
		cout<<endl;
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值