题目来源:
2021团体程序设计天梯赛
题目详情 - L2-039 清点代码库 (pintia.cn)
题目描述:
输入样例:
7 3
35 28 74
-1 -1 22
28 74 35
-1 -1 22
11 66 0
35 28 74
35 28 74
输出样例:
4
3 35 28 74
2 -1 -1 22
1 11 66 0
1 28 74 35
相关知识点:
map的遍历,map的查询
sort使用技巧:利用了 负号, 使得两个位置能按照相反的规则排序
vector的排序:vector是支持比较运算符的,按字典序排序
pair的查询(要和map对比记忆:map的语法是“->first”,pair的语法是“.first”)
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n, m;
map<vector<int>, int> mp;
//<功能模块,功能模块出现的次数> 目的:利用map统计次数
vector<pair<int, vector<int> > > ans;
//数组存放<功能模块出现的次数,功能模块> 目的:利用sort排序
int main(){
cin>>n>>m;
for(int i = 1; i<=n; ++i){
vector<int> temp;
for(int j = 1; j<=m; ++j){
int temp2; scanf("%d", &temp2);
temp.push_back(temp2);
}
mp[temp]++;
}
map<vector<int>, int>::iterator it;
for(it = mp.begin(); it != mp.end(); ++it){ //map的遍历
ans.push_back({-it->second, it->first}); //map的语法是“->first”
}
//不可用sort(ans.begin(), ans.end(), greater<pair<int, vector<int> > >());否则会打乱map中vector的顺序
//这里巧妙的利用了 负号, 使得两个位置能按照相反的规则排序
sort(ans.begin(), ans.end()); //次数相同的,对vector排序 ,vector是支持比较运算符的,按字典序排序
//遍历输出
printf("%d\n", ans.size());
for(int i = 0; i<ans.size(); ++i){
printf("%d", -ans[i].first); //pair的语法是“.first”
for(int j = 0; j<m; ++j){
printf(" %d", ans[i].second[j]);
}
printf("\n");
}
return 0;
}