2021团体程序设计天梯赛 L2-3 清点代码库
思路:
stl数据结构应用
Tip:
没啥好说的,做法千变万化,看自己的数据结构功底
#include <bits/stdc++.h> using namespace std; const int maxn = 10000 + 5; int n, m; struct mmp { int a[105]; int cnt = 0; } member[maxn]; bool cmp(mmp a, mmp b) { if (a.cnt == b.cnt) { bool flag = false; for (int i = 0; i < m; i++) if (a.a[i] != b.a[i]) return a.a[i] < b.a[i]; } return a.cnt > b.cnt; } map<vector<int>, int> mp; int main() { cin >> n >> m; int cnt = 0; for (int i = 1; i <= n; i++) { vector<int> v; for (int j = 0; j < m; j++) { int tmp; cin >> tmp; v.push_back(tmp); } if (mp.find(v) == mp.end()) { mp[v] = cnt; for (int j = 0; j < m; j++) member[cnt].a[j] = v[j]; member[cnt].cnt = 1; cnt++; } else { int nop = mp[v]; member[nop].cnt++; } } sort(member, member + cnt, cmp); cout << cnt << endl; for (int i = 0; i < cnt; i++) { cout << member[i].cnt; for (int j = 0; j < m; j++) cout << " " << member[i].a[j]; cout << endl; } return 0; }