• 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);
    	}
    }
    

      

  • 相关阅读:
    带你梳理Roach全量恢复的整体流程
    为AR&VR黑科技:以“自由视角”360度尽展舞台唯美
    一个15年的架构师谈“如何成为一名优秀的解决方案架构师”
    SARIF:DevSecOps工具与平台交互的桥梁
    “蚂蚁牙黑”太火,想玩就用ModelArts做一个!
    图神经网络,这到底是个什么?
    Python 面向对象进阶
    Python 装饰器
    SQL 存储过程中事务回滚
    python 迭代器协议和生成器
  • 原文地址:https://www.cnblogs.com/xieyizun-sysu-programmer/p/sicily1027.html
Copyright © 2020-2023  润新知