编辑代码

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
/*
哈希算法:
    将任意数据 和指定值创建一个关联关系,这样可以通过关联值找到对应数据。
    优点:查找和操作效率高O(1)
    缺点:打乱原有数据顺序
    场景:统计,筛选 ,高频的查找插入
    实现方式:数组,map
    哈希冲突

例题:
    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,
    判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。
    如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。
杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

bool  jundge(int chr[]){
     for (int i=0; i<26; i++){
        if (chr[i]<0) return false;    
    }
    return true;
}
  //输入
    string ransom, magazine;
    cin>>ransom>>magazine;
    //运算
    //1、创建哈希表,并进行初始化
    int chr[26]={0};
    //2、导入数据
    for (int i=0; i<magazine.size(); i++){
        int p = magazine[i]-'a';
        chr[p]++;
    }
    
    for (int i=0; i<ransom.size(); i++){
        int p = ransom[i]-'a';
        chr[p]--; 
    }
    
    //3、根据要求,操作哈希表
    bool b = true;
    for (int i=0; i<26; i++){
    
        if (chr[i]<0){
            b=false;
            break;
        }
    }
    //输出
    if(b) cout<<"true";
    else cout<<"false";
        cout<<nums.size()<<endl;
    nums[0].push_back("张三");
    cout<<nums[0][0]<<endl;
    nums[0].erase(find(nums[0].begin(),nums[0].end(),"张三"));
    cout<<nums[0].empty();
*/
int main() {
    //输入
    int n;
    string names[100];
    cin>>n;
    for ( int i=0; i<n; i++) cin>>names[i];

    //运算
    vector < vector <string> > nums(n);
    map <string, int> mp;
    for ( int i=0; i<n; i++){
        if (mp.find(names[i])!=mp.end()) mp[names[i]]++;
        else mp[names[i]] =1;
    }
    map <string, int> ::iterator it;
    for (it =mp.begin(); it!=mp.end(); it++) {
        string name = it->first;
        int  num = it->second;
        nums[num].push_back(name);
    }
    cout<<n;
    for (int i=n-1; i>=0; i--){

        if (nums[i].empty()) continue;

        cout<<"票数:"<<i<<":";
        for (int j=0; j<nums[i].size();j++) cout<<nums[i][j]<<" ";
        cout<<endl;
        
    }
	return 0;
}