C++中Map常见用法以及按照value排序
今天做一个简单的算法题,居然用了1个小时,STL unordered_map用多了,没想到map这次派上了用场,这里记录一下:
算法题为 给一个字符串例如 abaaba,每连续两个字符组成一个子串 ab, ba, aa,ab,ba,统计出现的个数,按照个数大小排序打印,这里 ab 2次,ba两次,aa 1次,如果都是一样的次数,按照字典顺序打印,例如这里ab 和ba都是2次,那么先打印ab 然后是ba 最后 是aa;
算法:找出字串,然后用map记录这个string和它出现的个数,注意要用map不能用unordered_map,这样能保证是按照字典排序,然后再用一个稳定排序按照这个Map的key排序即可。
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <map>
using namespace std;
typedef pair<string, int> PAIR;
int cmp(const PAIR &x, const PAIR &y)
{
return x.second > y.second;
}
void p (string& s) {
vector<string> vec;
;
; i < num; i++) {
);
vec.push_back(tmp);
}
sort (vec.begin(), vec.end());
map <string, int> tmp;
]= {};
] = {};
for (auto itr = vec.begin(); itr != vec.end(); itr ++) {
string val = *itr;
//cout << val << endl;
] - ] - 'a'] == true) {
tmp[val] = tmp[val] + ;
} else {
tmp[val] = ;
}
first[val[] - 'a'] = true;
sec[val[] - 'a'] = true;
}
vector <PAIR> pair_vec; //用pair来实现按照pair的第二个元素大小也就是value排序
for (auto it = tmp.begin(); it != tmp.end(); it++) {
pair_vec.push_back(make_pair(it->first, it->second));
}
stable_sort(pair_vec.begin(), pair_vec.end(), cmp);
for (auto curr = pair_vec.begin(); curr != pair_vec.end(); ++curr) {
cout << curr->first << endl;
}
}
int main() {
string s;
cin >> s;
p(s);
;
};
测试代码
mississippitennessee
输出:
ss
is
si
ee
en
es
ip
it
mi
ne
nn
pi
pp
se
te
C++中Map常见用法以及按照value排序的更多相关文章
- python map 常见用法
python map 常见用法2017年02月01日 19:32:41 淇怪君 阅读数:548版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/Tifficial/art ...
- STL map 常见用法详解
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...
- Linux中find常见用法
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- [转]Linux中find常见用法示例
Linux中find常见用法示例[转]·find path -option [ -print ] [ -exec -ok command ] {} \;find命令的参 ...
- Linux中 find 常见用法示例
Linux中find常见用法示例 #find path -option [ -print ] [ -exec -ok command ] {} \; #-print 将查找到的文件输出到标准输出 #- ...
- C语言 · C++中map的用法详解
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) map<string, int> Map ...
- STL中map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- C++11中map的用法
最全的c++map的用法 1. map最基本的构造函数:map<string ,int>mapstring; map<int,string >mapint;map<sri ...
- [转]Java中Map的用法详解
转载地址:http://www.zhixing123.cn/jsp/30113.html Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictio ...
随机推荐
- HDU 2073 无限的路
Problem Description 甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形: 甜甜的好朋友蜜蜜发现上面的图还 ...
- 批处理清理VS工程目录(递归删除Debug, Release, ipch目录及*.sdf文件)
用VS写程序最烦的就是VS会产生一大堆乱七八糟的东西,如Degub, Release, ipch目录,还有sdf文件,这些东西占了很大的空间,在linux下编程的话一个make clean就可以很方便 ...
- hdu3415 Max Sum of Max-K-sub-sequence
Max Sum of Max-K-sub-sequence Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64 ...
- “Will not add file alias 'samefile' ('SameFile' already exists in index)” when `git add/commit` operation
从远程仓库pull下来的代码有两个类类名首字母小写出现如下情况 然后我想删了重新写一下(就是把这个类删了,代码复制到名字正确的类里面),然后commit的时候出现这个错误,后来删一个commit一下, ...
- C++ 头文件系列(deque)
简介 deque是double ended queue(即双端队列)的简称. 就像C++中的大部分容器的一样,deque具有以下属性: 顺序的(sequence) 动态增长的(dynamic grow ...
- jmeter测试dubbo接口
本文讲解jmeter测试dubbo接口的实现方式,文章以一个dubbo的接口为例子进行讲解,该dubbo接口实现的功能为: 一:首先我们看服务端代码 代码架构为: 1:新建一个maven工程,pom文 ...
- Prefix the choice with ! to persist it to bower.json ? Answer (问你选择哪个1,2,3.........)
Unable to find a suitable version for underscore, please choose one by typing on e of the numbers be ...
- 写一个MyList
首先定义接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- c++ 常见问题之 const
const 默认状态下const对象仅在文件内有效,添加extern关键字可以在多个文件共享 const 引用: 可以把引用绑定到const对象上,对常量的引用不能被用作修改它所绑定的对象 const ...
- readelf -s 命令‘symbol’名字显示不全
GCC编译出来的object(目标文件)getPon.o,在链接时(ld)报了一个错误说找不到一个函数(undefined reference to identifier devCtl_getEthe ...