首页 > 其他分享 >L2-039 清点代码库

L2-039 清点代码库

时间:2024-03-22 19:14:09浏览次数:23  
标签:vector return int ++ 039 清点 first1 L2 first2

没有想到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;
}

参考博客: https://blog.csdn.net/Czyaun/article/details/104444522

标签:vector,return,int,++,039,清点,first1,L2,first2
From: https://www.cnblogs.com/chengyiyuki/p/18090266

相关文章

  • L2-038 病毒溯源
    #include<bits/stdc++.h>usingnamespacestd;vector<int>vec[10010],ans;//矩阵intvis[10010];intmaxLen=0;voiddfs(introot,vector<int>&v){ if(v.size()>maxLen){ ans.clear(); ans=v; maxLen=v.size(); } for(int......
  • MindSpore报错处理:TypeError: For 'set_context', the parameter device_id can not b
    问题背景在使用MindSpore运行一个分子动力学模拟的测试程序时:frommindsporeimportcontextfrommindspore.nnimportAdamif__name__=="__main__":importsyssys.path.insert(0,'../..')fromspongeimportSponge,Molecule,ForceField,set_global......
  • L2-036 网红点打卡攻略
    没有AC,没有用到每个地点只能打卡一次的限制条件。错误版本:#include<bits/stdc++.h>usingnamespacestd;intedges[210][210],fangan[2000][2000];intminspend=INT_MAX;intidx=0;intmain(){ intn,m; cin>>n>>m; for(inti=0;i<m;i++){ inta......
  • 中考英语首字母快速突破013-2021上海松江英语二模-Alice's Tiny Door: A Magical Jour
    PDF格式公众号回复关键字:ZKSZM013原文​Alicepickedagoldenkeyfromthetableandputitinallthelocksonthedoorsbutitdidn’topenanyofthem.Thenshediscoveredanotherdoor,averysmallone.Sheputthekeyinthelock.Itwasexactl......
  • C. Anji's Binary Tree
    网站:https://codeforces.com/problemset/problem/1900/C这里比较容易搞混的就是各个节点的关系,不要用2*n来表示左孩子!!以及记得添加快读快写ios::sync_with_stdio(false);cin.tie(nullptr);加在intmain()里即可这里还有一个priority_queue的小技巧:结构体内部定义运算符,好像和......
  • ESP8266 + L298N实现WIFI遥控小车
    参考资料:https://blog.csdn.net/m0_60790717/article/details/126914314https://blog.csdn.net/weixin_52801934/article/details/126180831 ESP8266实物和引脚图(图像来源于百度图片): L298N实物和引脚图(图像来源于百度图片): 硬件连接:L298N连接两个电机,来控制小车的两......
  • [转帖]Evaluating Garnet's Performance Benefits
    EvaluatingGarnet'sPerformanceBenefitsEvaluatingGarnet'sPerformanceBenefits|Garnet(microsoft.github.io) Wehavetested Garnet thoroughlyinavarietyofdeploymentmodes:SamelocalmachineforclientandserverTwolocalmachines-......
  • vue项目中使用html2canvas插件
    一、生成海报图vue项目中使用html2canvas插件,实现将编写的html代码转成可以保存的图片,只需要以下四步。1、在vue项目中安装插件npmihtml2canvas 2、在需要使用到的页面引入html2canvas插件importhtml2canvasfrom“html2canvas”; 3、按照设计图编写html代码<d......
  • ACCESS 关于使用VBA选择路径时提示"方法'FileDislog作用于对象'_Application’时失败"
    以下是源码:PrivateSubCommand0_Click()'打开文件选择对话框WithApplication.FileDialog(msoFileDialogFilePicker).AllowMultiSelect=False.Filters.Clear.Filters.Add"Excel文件","*.xls;*.xlsx",1I......
  • L2-034 口罩发放
    破防了,我自己写的只能得5分,测试点0都过不去,并且至今没有找到错误的原因。等我找到了再回来。然后看别人的。#include<bits/stdc++.h>usingnamespacestd;structnode{ stringname; stringtno; intstate; inttime; intpos;};map<string,int>mp;//领取资格ve......