L2-039 清点代码库
没有想到map的key可以是数组类型,本质是vector模板中运算符的重载。
1.==重载:
判断两个数组是一样的,含义是两个vector大小相同,并且固定位置上的值要相等。
//stl_vector.h
template <class T, class Alloc>
inline bool operator==(const vector<T, Alloc>& x, const vector<T, Alloc>& y) {
return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
}
//stl_algobase.h
template <class InputIterator1, class InputIterator2>
inline bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2) {
for ( ; first1 != last1; ++first1, ++first2)
if (*first1 != *first2)
return false;
return true;
}
2.<重载:
元素挨个比较,若第一个数组小于第二个数组对应位置上的值返回true。如果都相等,那最后第一个长度比第二个小,返回true。就是先比较值,值一样比较长度。
//stl_vector.h
template <class T, class Alloc>
inline bool operator<(const vector<T, Alloc>& x, const vector<T, Alloc>& y) {
return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}
//stl_algobase.h
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
for ( ; first1 != last1 && first2 != last2; ++first1, ++first2) {
if (*first1 < *first2) //比较数组中存储的值
return true;
if (*first2 < *first1)
return false;
}
return first1 == last1 && first2 != last2;
}
本题通过map直接就能去重。
写法一:
#include <bits/stdc++.h>
using namespace std;
map<vector<int>, int> mp;
bool cmp(pair<vector<int>,int> p1,pair<vector<int>,int> p2) {
if (p1.second != p2.second) {
return p1.second > p2.second;
}
for (int i = 0; i < p1.first.size(); i++) {//递增序排序
if (p1.first[i] != p2.first[i]) {
return p1.first[i] < p2.first[i];
}
}
return -1;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
vector<int> vec;
for (int j = 0; j < m; j++) {
int output;
cin >> output;
vec.push_back(output);
}
mp[vec]++;//次数增加
}
//放到vector当中进行排序
vector < pair<vector<int>, int>> res;//模块是什么 模块出现的次数
for (auto x : mp) {
res.push_back(x);
}
sort(res.begin(), res.end(), cmp);
cout << res.size() << '\n';
for (int i = 0; i < res.size(); i++) {
vector<int> x = res[i].first;
int cnt = res[i].second;
cout << cnt << " ";
for (int j = 0; j < x.size(); j++) {
cout << x[j];
if (j < x.size() - 1) cout << " ";
}
cout << '\n';
}
return 0;
}
写法二:
#include <bits/stdc++.h>
using namespace std;
struct node {
vector<int> a;
int cnt;//出现的次数
};
bool cmp(node n1,node n2) {
if (n1.cnt != n2.cnt) return n1.cnt > n2.cnt;
for (int i = 0; i < n1.a.size(); i++) {
if (n1.a[i] != n2.a[i]) {
return n1.a[i] < n2.a[i];
}
}
return -1;
}
vector<node> vc;
map<vector<int>, int> mp;
int main() {
int n, m;
cin >> n >> m;
vc.resize(n + 10);
int index = 1;
for (int i = 0; i < n; i++) {
vector<int> t;//模板解
for (int j = 0; j < m; j++) {
int s;
cin >> s;
t.push_back(s);
}
if (mp[t]) {//已经存在
vc[mp[t]].cnt++;//次数增加一
}
else {//第一次出现
node node;
node.a = t;
node.cnt = 1;
vc[index] = node;
mp[t] = index;//放的是下标
index++;
}
}
sort(vc.begin()+1, vc.begin()+index, cmp);
cout << (index -1) << endl;
for (int i = 1; i < index;i++) {
vector<int> out = vc[i].a;
int cnt = vc[i].cnt;
cout << cnt << " ";
for (int j = 0; j < out.size(); j++) {
cout << out[j];
if (j < out.size() - 1) cout << " ";
}
cout << '\n';
}
return 0;
}
参考博客: https://blog.csdn.net/Czyaun/article/details/104444522