class Solution {
public:
/**
* return topK string
* @param strings string字符串vector strings
* @param k int整型 the k
* @return string字符串vector<vector<>>
*/
// 自定义排序规则,先按照second从大到小排序、如果second相等的情况下,按照first从小到大排序
struct cmp{
bool operator() (const pair<string, int>& p1, const pair<string, int>& p2) {
return (p1.second < p2.second) || (p1.second == p2.second && p1.first > p2.first);
}
};
vector<vector<string> > topKstrings(vector<string>& strings, int k) {
// write code here
int n = strings.size();
vector<vector<string> > res;
unordered_map<string, int> mp;
for(auto s:strings)
mp[s]++;
priority_queue<pair<string, int>, vector<pair<string, int>>, cmp> q;
int i = 0;
// 将所有的数据排序
for(auto m:mp)
q.push(m);
// 输出前k个数值
while(i < k) {
res.push_back({q.top().first, to_string(q.top().second)});
q.pop();
i++;
}
return res;
}
};