Sicily 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

Problem Source

ZSUACM Team Member

这道题的思路同样比较简单,每一次给出n组字符串,每组包括BBS账号和IP地址,每一个IP地址对应两个不同的BBS账号,先给出的BBS账号(main_ID)是后给出的BBS账号(MJ_ID)的MaJia,然后按照main_ID的字典顺序输出所有对应序列即可。

首先建立一个<string, string>类型的map关联m储存BBS-IP对应关系,建立结构体数组s储存所有BBS之间的对应关系;

然后遍历全部BBS-IP对应关系,当一个IP第一次出现时其m关联字符串为空,此时赋予其对应的BBS的值;当一个IP第二次出现即其关联字符串不为空时,此时IP已经关联的BBS即为main_ID,刚刚遍历到的BBS值即为mj_ID,将其写入结构体数组中,数组跳到下一储存单位即可,通过计数器p来计算最终得到的结构体数组s的数目;

最后自定义cmp对结构体数组s进行排序,关键字为main_ID,将其sort排序后,按照题目要求输出即可,需要注意的是每一个测试样例最后都要有一个空行,不要漏掉。

代码如下:

#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;
}

  

时间: 2024-10-14 22:07:08

Sicily 1027 MJ,Nowhere to Hide的相关文章

sicily 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

Sicily 1027. MJ, Nowhere to Hide

//就是一个简单的字符串配对~~用map来解决很easy #include <iostream> #include <map> #include <string> using namespace std; int main() { int n; while (cin>>n && n!=0) { string ip , name; string tmp; map<string,string> m; map<string,str

SOJ 1027 MJ,Nowhere to Hide

题目大意:输入n行(id, ip)对,对同一个ip,第一次出现的ip对应的id为Main_ID, 第二次出现的ip对应的id为MJ_ID, 输出n / 2个(Main_ID, MJ_ID)对,输出格式为 MJ_ID is the MaJia of Main_ID,输出按Main_ID的字典序输出.当n为0时,输入结束. 解题思路:这是一种映射问题,用数据结构map非常合适.每次读入(id, ip) 对,检查map中是否已存在键值为ip的键,若不存在,则插入(id, ip)对,若存在则存放到结果映

android 如何引用@hide(隐藏)的类,方法和常量?

最近在处理SD卡的读写问题,本地Elipse能跑的程序提交到服务器上的时候,报错,找不到import的类,还有一些方法也是找不到,利用sourceInsight搜了源码发现都是用了@hide标记的类和方法. /** * Description of a storage volume and its capabilities, including the * filesystem path where it may be mounted. * * <span style="color:#ff

Android Fragment 的使用,一些你不可不知的注意事项

Fragment,俗称碎片,自 Android 3.0 开始被引进并大量使用.然而就是这样耳熟能详的一个东西,在开发中我们还是会遇见各种各样的问题,层出不穷.所以,是时候总结一波了. Fragment 简介 作为 Activity 界面的一部分,Fragment 的存在必须依附于 Activity,并且与 Activity 一样,拥有自己的生命周期,同时处理用户的交互动作.同一个 Activity 可以有一个或多个 Fragment 作为界面内容,并且可以动态添加.删除 Fragment,灵活控

jQuery 3.0最终版发布,十大新特性眼前一亮

jQuery 3.0在日前发布了最终的全新版本.从2014年10月,jQuery团队对这个主要大版本进行维护开始,web开发者社区便一直在期待着这一刻的到来,终于在2016年6月他们迎来了这一个最终版www.lampbrother.net. 通过jQuery 3.0的版本更新说明,我们看到了一个保持着向后兼容的更轻便,更快速的jQuery.在本文中,我们将介绍一些令人眼前一亮的jQuery 3.0全新特性. 开始前的说明 如果你想要下载jQuery 3.0进行亲自实验,可以通过该页面进行下载.另

JQuery: JQuery效果(隐藏、显示、切换,滑动,淡入淡出,以及动画)

JQuery:效果 JQuery效果有很多,包括隐藏.显示.切换,滑动,淡入淡出,以及动画等.隐藏:JQuery hide() 显示:JQuery show() 切换:JQuery toggle() 淡入淡出:JQuery fadeIn().JQuery fadeOut().JQuery fadeToggle().JQuery fadeTo() 滑动:JQuery slideDown().JQuery slideUP().JQuery slideToggle() 动画:JQuery animat

Android 开发 对话框Dialog dismiss和hide方法的区别

http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以用show方法调用显示.但是,这两者是有区别的. dismiss方法会释放对话框所占的资源,而hide方法不会.activity退出前必须调用dismiss方法关闭对话框.如果对话框上有progressbar,你会发现,调用dismiss方法后,再调用show方法,出来的对话框,上面的progres

sicily 1063. Who&#39;s the Boss

Time Limit: 1sec    Memory Limit:32MB Description Several surveys indicate that the taller you are, the higher you can climb the corporate ladder. At TALL Enterprises Inc. this "de facto standard" has been properly formalized: your boss is alway