# The real topic of the 6th TIANTI competition - L2-3 counting code base (vector,map)

Posted by CoolAsCarlito on Fri, 18 Feb 2022 22:04:17 +0100

L2-039 inventory code base (25 points)

The above picture is transferred from Sina Weibo: "there are hundreds of millions of lines of code in Alibaba code base, but there are many codes with repeated functions. For example, fast sorting alone has been rewritten hundreds of times. Please design a program that can find out all the codes with repeated functions in the code base. What do you think, I was confused at that time, and then hung up..."

Here, let's simplify the problem: first, suppose that if two functional modules accept the same input and always give the same output, then they are functionally repeated; Secondly, we simplify the output of each module to an integer (in the range of int). So we can design a series of inputs and check the corresponding outputs of all functional modules, so as to find out the codes with duplicate functions. Your task is to design and implement a solution to this simplified problem.

Input format:
Input: two positive integers are given in the first line, which are N (≤ 10 ^ 4)
) and M (≤ 10 ^ 2), corresponding to the number of functional modules and the number of series test inputs.

Then N lines, each line gives M corresponding outputs of a functional module, and the numbers are separated by spaces.

Output format:
First, output the number k of different functions in the first line. Then K lines, each line gives the number of modules with this function and the corresponding output of this function. The numbers shall be separated by one space, and there shall be no extra space at the beginning and end of the line. The output is first given in the non increasing order of the number of modules. If there are parallels, it is given in the increasing order of the output sequence.

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
```

Output example:

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

When I saw this question during the competition, my first reaction was that I could use string to save the output of the function, and then use map to record it. But map can't sort, so I thought for a while and came up with a little trick. Use the constructor of vector to copy map and sort. See code

```#include "bits/stdc++.h"
using namespace std;
map<string ,int> m;
int n,m1,num[10010];
bool cmp(pair<string,int> a,pair<string,int> b)//Define collation
{
if(a.second!=b.second) return a.second>b.second;
else return a.first<b.first;
}
int main()
{
cin>>n>>m1;
getchar();
while(n--)
{
string a;//I read it directly in a string
getline(cin,a);
if(m.find(a)==m.end()) m[a]=1;//If you can't find it, insert it directly
else m[a]++;//Otherwise++
}
cout<<m.size()<<endl;
vector<pair<string,int> > v(m.begin(),m.end());//Use vector to convert the map. Since the map can't be sorted, I'll replace it with vector!
sort(v.begin(),v.end(),cmp);
for(auto i:v)	cout<<i.second<<' '<<i.first<<endl; //auto is an automatic variable introduced by C11 to automatically identify parameter types
}
```

Just when I wanted to get full marks, the running results came out

I'm so stupid that I can't think of where it can block my point. Finally, I decided not to be lazy. Since it gave me m, I'd better use it.
The change is also very simple. Just change the string to vector
See code

```#include "iostream"
#include "map"
#include "vector"
#include "algorithm"
using namespace std;
map<vector<int> ,int> m;//Replace string with vector
int n,m1,num[10010];
bool cmp(pair<vector<int>,int> a,pair<vector<int>,int> b)
{
if(a.second!=b.second) return a.second>b.second;
else return a.first<b.first;
}
int main()
{
cin>>n>>m1;
getchar();
while(n--)
{
vector<int> a;
for(int i=0;i<m1;i++)
{
int b;
cin>>b;
a.push_back(b);
}
m[a]++;
}
cout<<m.size()<<endl;
vector<pair<vector<int>,int> > v(m.begin(),m.end());//It's all the same
sort(v.begin(),v.end(),cmp);
for(auto i:v)
{
cout<<i.second;
for(int j=0;j<m1;j++)
cout<<' '<<i.first[j];
cout<<endl;
}
}
```

I nervously ordered the submission, and the result
Although I wrote it out, I still don't understand where it's stuck. Is there anything special about string and vector that the compiler can distinguish?
If a big man knows, he can point out his little brother in the comment area
In a word, the problem is solved, cherr!
I am Qianmo, an amateur player playing ACM. If you like this blog, just like it before you go!

Topics: Permutation STL pta map