Phone chat madman (25 points) (a simple method of map mapping)

Given the call records of a large number of mobile phone users, find the chat madman with the most calls.

Input format: The
input first gives a positive integer N (≤10​5​​), which is the number of call records. Next N lines, each line gives a call record. For the sake of simplicity, only the 11-digit mobile phone numbers of the calling party and the receiving party are listed here, separated by spaces.

Output format:
Give the mobile phone number of the chat madman and the number of calls in one line, separated by spaces. If such a person is not unique, the smallest number among the madmen and the number of calls are output, and the number of parallel madmen is additionally given.

Input sample:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832
13588625832 3

Question ideas:
1. First think of map mapping, let the number of corresponding numbers as the second value, and then consider sorting the map container according to the second value. It should be noted here that the sort function cannot be used directly for sorting, and the map cannot be sorted with Sort matches. In addition, the map container sorts the size of the key value. For this problem, we need to sort according to the size of the value.
2. Therefore, we need to achieve the purpose of sorting. We need to put the things in the map container into pair<key, value>, and use pair<key, value> as the type of vector. At this time, we can customize the mycmp function. The vector is sorted. If you don't understand, just look at the code, it's simple and clear.
3. If you do not understand the relevant knowledge points, it is recommended to study the relevant content, because these things are more important. I hope this article can help you a bit. If you don’t understand, you can private message or comment.

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
map<string, int> mp;		//一开始存放数据的map容器
vector<pair<string, int>> vec;		//能够进行排序的vector容器,数据类型是pair<>
bool mycmp(pair<string, int>a, pair<string, int>b) {
    
     return a.second > b.second; };//自定义的比较函数
int main()
{
    
    
	int n, len;
	cin >> n;
	len = 2 * n;
	string s;
	for (int i = 0; i < len; i++)		//第一步把数据存到map容器中
	{
    
    
		cin >> s;
		mp[s]++;
	}		

	for (auto it = mp.begin(); it != mp.end(); it++)		//第二步,把map容器里的内容放到vector<pair<sting, int>>中
		vec.push_back(pair<string, int>(it->first, it->second));
	
	sort(vec.begin(), vec.end(), mycmp);		//排序
	int maxn = vec[0].second;
	int sum = 1;
	for (int i = 1; i < vec.size(); i++)		//查找聊天狂人有几个
	{
    
    
		if (vec[i].second == maxn)
			sum++;
		else
			break;
	}

	if (sum == 1)								//按照题目要求输出即可
		cout << vec[0].first << " " << vec[0].second << endl;
	else
		cout << vec[0].first << " " << vec[0].second << " " << sum << endl;
	
	return 0;
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/114019653