【天梯赛】L2-039 清点代码库** (25 point(s))

本文对比了使用string和vector存储数据在编程竞赛中的优劣,并分享了通过vector存储vector<int>类型数据以提高代码效率并成功通过样例的经历。重点在于代码优化和数据结构选择对解决问题的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值