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

#include<iostream>
#include<map>
#include <string>
using namespace std;

void swap(string&, string&);
void qsort(string [], int, int, string []);
int main() {
	int n = 0;
	while (cin>>n && n != 0) {
		string id, ip;
		map<string,string> acmers;
		string former[n/2];
		string latter[n/2];
		int j = 0;
		for (int i = 0; i < n; i++) {
			cin >> id >> ip;
			//使用map的支持小标为string型key,来达到直接字符串查找功能,
			if (acmers.count(ip)==1) { //如果map中已经存在一个key为ip的项,则count为1,否则为0,从而判断出是否存在匹配
				former[j] = id + " is the MaJia of ";
				latter[j] = acmers[ip];
				j++;
			} else {
				acmers[ip] = id;
			}
		}
		qsort(latter, 0, j-1, former);
		for (int i = 0; i < n/2; i++) {
			cout << former[i] + latter[i] << endl;
		}
		cout << endl;
	}
	return 0;
}
//字符串交换
void swap(string &a, string &b) {
	string temp = a;
	a = b;
	b = temp;
}
//快排实现字符串排序
void qsort(string latter[], int low, int height, string former[]) {
	if (low < height) {
		string lpvt = latter[(low+height)/2];
		string fpvt = former[(low+height)/2];
		int p = low;
		swap(latter[(low+height)/2], latter[height]);
		swap(former[(low+height)/2], former[height]);
		for (int i = low; i < height; i++) {
			if (latter[i].compare(0,latter[i].length(), lpvt) < 0) {
				swap(latter[i], latter[p]);
				swap(former[i], former[p]);
				p++;
			}
		}
		swap(latter[p], latter[height]);
		swap(former[p], former[height]);

		qsort(latter, low, p-1, former);
		qsort(latter, p+1, height, former);
	}
}

  

时间: 2024-08-28 13:48:45

sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序的相关文章

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

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

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)对,若存在则存放到结果映

uva:10340 - All in All(字符串匹配)

题目:10340 - All in All 题目大意:给出字符串s和t,问s是否是t的子串.s若去掉某些字符能和t一样,那么t是s的子串. 解题思路:匹配字符.t的每个字符和s中的字符匹配.注意这里的字符数组大小要开大点. 代码: #include <stdio.h> #include <string.h> const int N = 1000005; char s[N], t[N]; bool match () { int i = 0; int lens = strlen(s);

CCF 字符串匹配

问题描述 试题编号: 201409-3 试题名称: 字符串匹配 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一个字母的大写和小写看作相同的字符. 输入格式 输入的第一行包含一个字符串S,由大小写英文字母组成. 第二行包含一个数字,表示大小写敏感的选项,当数字为0时表示大小写不敏感,当数字为1时表示大小

字符串匹配的KMP算法

html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, "Microsoft YaHei UI", SimSun, SimHei, arial, sans-serif; line-height: 1.6; color: ; background-color: ; margin: 0; padding: 16px 20px; } h1, h2, h

字符串匹配与KMP算法笔记

>>字符串匹配问题 字符串匹配问题即在匹配串中寻找模式串是否出现, 首先想到的是使用暴力破解,也就是Brute Force(BF或蛮力搜索) 算法,将匹配串和模式串左对齐,然后从左向右一个一个进行比较, 如果不成功则模式串向右移动一个单位,直到匹配成功或者到达匹配串最后仍然不成功,返回失败. 很明显,这种算法有很多的地方可以优化,假设要搜索的串为S,长度为n,要匹配的串为M,长度为m,时间复杂度为O(nm). >>KMP算法 Knuth-Morris-Pratt算法以三个发明者命名

KMP算法解决字符串匹配

该算法由D.E.Knuth ,J.H.Morris和 V.R.Pratt提出,用于解决字符串匹配问题. 思想: 设目标串(主串)为s,模式串为t ,并设i指针和j指针分别指示目标串和模式串中正待比较的字符,设i和j的初值均为0.若有s[i]=t[j],则i和j分别加1.否则,i不变,j退回到j=next[j-1]的位置,再比较s[i]和t[j],若相等,则i和j分别加1.否则,i不变,j再次退回到j=next[j]的位置,依此类推.直到下列两种可能: 1. 模式串t中的字符全部匹配,则出现频率+

字符串匹配 【kmp】

字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 214 Solved: 81 Description 给你两个字符串A,B,请输出B字符串在A字符串中出现了几次. Input 多组测试数据,每组输入两个字符串.字符串的长度 <= 1000000. Output 输出B在A中出现的次数. Sample Input aaa aa Sample Output 1 子串在母串中出现的次数,串不重叠 #include <stdio.h> #