PTA L2-039 Inventory Code Library (STL)

4.18 I just finished the China University Computer Contest-Group Programming Ladder Competition. I participated in the GPLT for the first time, and I was 9 points away from the second place. Ahhh, I am a good dish (QAQ). I should try another question of L3 .
insert image description here

Then if you have time, pick a few questions and post ideas and code

[Problem Description]
First assume that if two functional modules accept the same input and always give the same output, they are functionally repetitive; secondly, we simplify the output of each module to an integer (in int intint range ) .
So we can design a series of inputs and check the corresponding outputs of all functional modules, so as to find codes with duplicate functions. Your task is to design and implement a solution to this simplified problem.

【Input format】
Input is given in the first line 2 22 positive integers, followed byNNN andMMM , corresponds to the number of function modules and the number of series test inputs.
ThenNNN lines, each line givesthe MMM corresponding outputs, the numbers are separated by spaces.

[Output format]
First output the number KK of different functions in the first lineK. _
Followed by K lines, each line gives the number of modules with this function, and the corresponding output of this function. Numbers are separated by a space, and there must be no extra spaces at the beginning and end of the line.
The output is first given in non-increasing order of the number of modules, and if there is a tie, it is given in increasing order of the output sequence.
Note: The so-called sequenceA 1 , … , AM {A_1,\dots ,A_M}A1,,AM B 1 , … , B M {B_1,\dots ,B_M} B1,,BMLarge, means that there is 1 ≤ i < M 1≤i<M1i<M , useA 1 = B 1 , … , A i = B i A_1=B_1,\dots ,A_i=B_iA1=B1,,Ai=Biestablished, and A i + 1 > B i + 1 A_{i+1}>B_{i+1}Ai+1>Bi+1

【Data range】
1 ≤ N ≤ 1 0 4 1≤N≤10^41N104
1 ≤ M ≤ 100 1≤M≤100 1M100

【Input sample】

7 3
35 28 74
-1 -1 22
28 74 35
-1 -1 22
11 66 0
35 28 74
35 28 74

【Example of output】

4
3 35 28 74
2 -1 -1 22
1 11 66 0
1 28 74 35

【analyze】


This question examines STL STLThe use of STL , for each set of input data, store itvectorand map it mapto the top , and count the number of times it appears. At this time , it has been sorted according to the lexicographical ordermap. vectorThenstoremapeach pair of elementsin , and put the number of occurrences of each group of data in, sinceit is sorted from small to large according to,so you need to add a negative sign before the number of times when storing. Thenjustthe finalresultpairvectorfirstsortpairfirstsort
sort(ans.begin(), ans.end(), greater<pair<int, vector<int> > >());


【Code】

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;

const int N = 10010, M = 110;
map<vector<int>, int> cnt;
vector<pair<int, vector<int> > > ans;
int n, m;

int main()
{
    
    
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
	{
    
    
		vector<int> temp;
		for (int j = 0; j < m; j++)
		{
    
    
			int x;
			scanf("%d", &x);
			temp.push_back(x);
		}
		cnt[temp]++;
	}
	for (auto &u : cnt) ans.push_back({
    
     -u.second, u.first });
	//for (auto &[u, v] : cnt) ans.push_back({ -v, u });//C++新特性,PTA不支持
	sort(ans.begin(), ans.end());
	printf("%d\n", cnt.size());
	for (auto &u : ans)
	{
    
    
		printf("%d", -u.first);
		for (auto &v : u.second)
			printf(" %d", v);
		puts("");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_51755720/article/details/116457511#comments_26012019