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);
- }
- }
sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序的更多相关文章
- Sicily 1027. MJ, Nowhere to Hide
//就是一个简单的字符串配对~~用map来解决很easy #include <iostream> #include <map> #include <string> ...
- 字符串匹配的KMP算法
~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 字符串匹配(hash算法)
hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- sdut 2125串结构练习--字符串匹配【两种KMP算法】
串结构练习——字符串匹配 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- 字符串匹配--Karp-Rabin算法
主要特征 1.使用hash函数 2.预处理阶段时间复杂度O(m),常量空间 3.查找阶段时间复杂度O(mn) 4.期望运行时间:O(n+m) 本文地址:http://www.cnblogs.com/a ...
- 字符串匹配的KMP算法详解及C#实现
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
随机推荐
- IOS编程之通讯录
// // ViewController.m // LearnAddressBook0 // // Created by Mac on 14-7-28. // Copyright (c) 20 ...
- 分割文件命令split
使用Linux自带的split命令,可以将很大的文件分割成若干个小文件,以方便传送和使用. 命令格式: split [option] [input file] [output file] 常用选项: ...
- Bash脚本编程基础
为实现某个任务,将许多命令组合后,写入一个可执行的文本文件的方法,称为Shell脚本编程. 按照应用的Shell环境不同,可以将Shell脚本分为多种类型.其中最常见的是应用于Bash和Tcsh的脚本 ...
- php 换行 空格分割处理
<?php function parse_specification($specification){ $rt=array(); $lines=array_filter(preg_split(& ...
- STC51系列单片机免掉电下载(热启动下载)
相信喜欢单片机的朋友都用过STC的单片机,用过STC单片机的朋友都有这种感受:实惠.易用.功能强大!就是每次下载都要冷启动特别恶心,相信很多朋友的开发板上的电源键都按烂了. 其实STC单片机可以不用免 ...
- 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 4.全局防护Bypass之二次注入
0x01 背景 现在的WEB程序基本都有对SQL注入的全局过滤,像PHP开启了GPC或者在全局文件common.php上使用addslashes()函数对接收的参数进行过滤,尤其是单引号.二次注入也是 ...
- DIH中添加不同的数据源
需求:从mysql数据库中读取一个知识记录,从记录表中的字段值中获取一个文件路径,读取xml文件,xml文件中可能包含多个文档内容.建立索引. xml文件样例: <?xml version=&q ...
- 通用链表实现(参考Linux List)
最近参考Linux实现的通用双向链表时,因typeof并不是标准c规定的关键字,除GCC编译器外其他编译器未必支持typeof关键字,所以在使用上并不能想Linux所实现的链表哪样灵活,它要求将连接器 ...
- PHP实现遍历、复制、删除目录
一.遍历 opendir 具体函数我就不解释了,直接看代码理解: <?php header("Content-Type:Text/html;charset=utf8"); $ ...
- RandomAccessFile的使用
package com.lk.C; import java.io.IOException; import java.io.RandomAccessFile; public class RandomAc ...