sicily-1027. MJ, Nowhere to Hide

1027. MJ, Nowhere to Hide

限制条件

时间限制: 1 秒, 内存限制: 32 兆

题目描述

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.

输入格式

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.

输出格式

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.

样例输入

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

样例输出

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

题目来源

ZSUACM Team Member

这道题的主要意思是,有多组测试样例,每组第一行输入为n,以0结束。接下来是n 个BBS 的post。它的形式是BBS_ID IP_Address

我们的主要任务是匹配IP相同的ID。输出有n/2行,每一行的格式是:MJ_ID is the MaJia of main_ID。其中main_ID是第一个出现的BBS_ID。输出按字典序排列。

这道题没什么好讲的,查找排序就行。

代码如下:

//1027. MJ, Nowhere to Hide
//2016.12.02

#include <iostream>
#include <cstring>

using namespace std;

struct IP
{
	char BBS_ID[20];
	char IP_Address[20];
	char MJ_ID[20];
	bool flag;   //标记IP是否是第一次出现
};

IP k[22];
IP p[11];  

int main()
{
	int n;
	while(cin>>n && n!=0)
	{
		for(int i=0;i<n;i++)
		{
			cin>>k[i].BBS_ID >>k[i].IP_Address;
			k[i].flag = true;
		}
		
		int x=0;
		
		for(int i=0;i<n;i++)
		{
			if(k[i].flag)
			{
				//查找相同项
				for(int j=i+1;j<n;j++)
				{
					if(strcmp(k[i].IP_Address  ,k[j].IP_Address )==0)
					{
					
						p[x++]=k[i];
						strcpy(p[x-1].MJ_ID ,k[j].BBS_ID);
						k[j].flag = false;
					}
				}
			}
		}
//排序
		for(int i=0;i<x;i++)
		{
			IP temp;
			for(int j=i;j<x;j++)
			{
				if(strcmp(p[i].BBS_ID,p[j].BBS_ID)>0)
				{
					temp=p[i];
					p[i]=p[j];
					p[j]=temp;
					
				}
			}
		}
		for(int i=0;i<x;i++)
		{
			cout<<p[i].MJ_ID <<" is the MaJia of "<<p[i].BBS_ID<<endl;
		}
		cout<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值