用string来存的话,至少我是只过了样例,答案显示多种错误。
下面使用vector的形式来存储,比string的方法码量小,最重要的是过了。泪目
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
map<vector<int>,int> mp;
int n,m;
struct node{
int cnt;
vector<int> nu;
}a[N];
bool cmp(node a, node b)
{
if(a.cnt == b.cnt){
for(int i = 0; i < m; i++){
if(a.nu[i] != b.nu[i]){
return a.nu < b.nu;
}
}
}
return a.cnt > b.cnt;
}
int main(){
cin >> n >> m;
getchar();
string s;
vector<int> bbb;
while(n--){
int num;
for(int i = 0; i < m; i++){
cin >> num;
bbb.push_back(num);
}
mp[bbb]++;
bbb.clear();
}
map<vector<int>,int>::iterator it;
int i = 0;
for(it = mp.begin(); it!=mp.end(); it++){
a[i].nu = it->first;
a[i++].cnt = it->second;
//可能因.nu为vector<int>的形式,所以无法输出
// cout << it->first<<" " << a[i - 1].cnt << endl;
// cout << a[i - 1].nu <<" " << a[i-1].cnt << endl;
}
sort(a,a + i,cmp);
cout << mp.size() << endl;
for(int j = 0; j < i; j++){
cout << a[j].cnt <<" ";
for(int k = 0; k < m; k++){
if(k != m - 1){
cout << a[j].nu[k] <<" ";
}
else{
cout << a[j].nu[k]<<endl;
}
}
}
return 0;
}
之前注释的string代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
map<vector<int>,int> mp;
int n,m;
struct node{
int cnt;
vector<int> nu;
// string str;
//如果用string的方法十分复杂
}a[N];
//bool cmp(node a, node b){
// if(a.cnt == b.cnt){
// int t = 0,t1 = 0;
// vector<int> aa,bb;
// int len = a.str.size(), lenb = b.str.size();
// for(int i = 0; i < len; i++){
// if(a.str[i] != ' '){
// if(a.str[i] != '-')
// t += t * 10 + (a.str[i] - '0');
// else{
// t *= -1;
// }
// }
// else{
// aa.push_back(t);
// t = 0;
// }
// if(a.str[i] != ' '){
// if(a.str[i] != '-')
// t += t * 10 + (a.str[i] - '0');
// else{
// t *= -1;
// }
// }
// }
// if(t) aa.push_back(t);
// for(int i = 0; i < lenb; i++){
// if(b.str[i] != ' '){
// if(b.str[i] != '-')
// t1 += t1 * 10 + (b.str[i] - '0');
// else{
// t1 *= -1;
// }
// }
// else{
// bb.push_back(t1);
// t1 = 0;
// }
// if(b.str[i] != ' '){
// if(b.str[i] != '-')
// t1 += t1 * 10 + (b.str[i] - '0');
// else{
// t1 *= -1;
// }
// }
// }
// if(t1) bb.push_back(t1);
// for(int i = bb.size() - 1; i >= 0; i--){
// if(bb[i] != aa[i]){
// return aa[i] < bb[i];
// }
// }
// }
// return a.cnt > b.cnt;
//}
bool cmp(node a, node b)
{
if(a.cnt == b.cnt){
for(int i = 0; i < m; i++){
if(a.nu[i] != b.nu[i]){
return a.nu < b.nu;
}
}
}
return a.cnt > b.cnt;
}
int main(){
cin >> n >> m;
getchar();
string s;
vector<int> bbb;
while(n--){
int num;
for(int i = 0; i < m; i++){
cin >> num;
bbb.push_back(num);
}
mp[bbb]++;
bbb.clear();
// getline(cin,s);
// mp[s]++;
}
map<vector<int>,int>::iterator it;
int i = 0;
for(it = mp.begin(); it!=mp.end(); it++){
a[i].nu = it->first;
a[i++].cnt = it->second;
//可能因.nu为vector<int>的形式,所以无法输出
// cout << it->first<<" " << a[i - 1].cnt << endl;
// cout << a[i - 1].nu <<" " << a[i-1].cnt << endl;
}
sort(a,a + i,cmp);
cout << mp.size() << endl;
for(int j = 0; j < i; j++){
cout << a[j].cnt <<" ";
for(int k = 0; k < m; k++){
if(k != m - 1){
cout << a[j].nu[k] <<" ";
}
else{
cout << a[j].nu[k]<<endl;
}
}
}
// int len1 = a[j].str.size();
// int t = 0;
// for(int k = 0; k < len1; k++){
// if(a[j].str[k] !=' '){
// if(a[j].str[k] =='-'){
// t *= -1;
// }
// else{
// t += t*10 + (a[j].str[k] - '0');
// }
// }
// else{
// cout << t <<" ";
// }
// }
// if(t) cout<<t << endl;
//// for(int ) << a[j].str << endl;
// }
return 0;
}