1027. MJ, Nowhere to Hide
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.
Problem Source
ZSUACM Team Member
好麻烦的方法。没手感。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int t;
while(cin>>t&&t)
{
string arr[21][2],name[21][2];
for(int k=0;k<t;k++)
cin>>arr[k][0]>>arr[k][1];
for(int i=0;i<t;i++)
{
for(int j=0;j<t-i-1;j++)
{
if(arr[j][1]>arr[j+1][1])
{
string temp1=arr[j][1];
arr[j][1]=arr[j+1][1];
arr[j+1][1]=temp1;
string temp2=arr[j][0];
arr[j][0]=arr[j+1][0];
arr[j+1][0]=temp2;
}
}
}
int count=0;
for(int i=0;i<t;i+=2)
{
name[count][0]=arr[i][0];
name[count][1]=arr[i+1][0];
count++;
}
for(int i=0;i<count;i++)
{
for(int j=0;j<count-i-1;j++)
{
if(name[j][0]>name[j+1][0])
{
string temp1=name[j][1];
name[j][1]=name[j+1][1];
name[j+1][1]=temp1;
string temp2=name[j][0];
name[j][0]=name[j+1][0];
name[j+1][0]=temp2;
}
}
}
/*for(int i=0;i<count;i++)
cout<<name[i][0]<<" "<<name[i][1]<<endl;*/
for(int i=0;i<count;i++)
cout<<name[i][1]<<" is the MaJia of "<<name[i][0]<<endl;
cout<<endl;
}
return 0;
}