牛客:HJ26 字符串排序[华为机考][map]

学习要点

  1. multimap.equal_range

题目链接

字符串排序_牛客题霸_牛客网

题目描述

解法:multimap

cpp 复制代码
#include <bits/stdc++.h>
#include <cctype>
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    string line_big_str;
    getline(cin,line_big_str);
    multimap<char,vector<int>> ch_bool_pos_map;
    vector<char> ret_ch(line_big_str.size(),'a');
    for(int i = 0;i<line_big_str.size();i++)
    {
        if(islower(line_big_str[i]))
        {
            // 构造值数组
            vector<int> tv;
            tv.push_back(1); tv.push_back(i);
            // 构造键值对:小写字母
            ch_bool_pos_map.insert({line_big_str[i],tv});
        }
        else if(isupper(line_big_str[i]))
        {
            // 构造值数组
            vector<int> tv;
            tv.push_back(0); tv.push_back(i);
            // 构造键值对:大写字母
            ch_bool_pos_map.insert({tolower(line_big_str[i]),tv});
        }
        else {
            // 确定非字母字符位置
            ret_ch[i] = line_big_str[i];
        }
    }
    // 写入ret_ch
    vector<char> t_ret;
    for(char ch = 'a';ch<='z';ch++)
    {
        auto it1 = ch_bool_pos_map.find(ch);
        if(it1 == ch_bool_pos_map.end())
        {
            continue;
        }
        else
        {
            // 构造单桶map
            auto it2 = ch_bool_pos_map.equal_range(ch);
            map<int,char> zi_map;
            for(auto i = it2.first;i != it2.second;i++)
            {
                zi_map[i->second[1]] = i->second[0] == 1 ? ch : toupper(ch);
            }
            // 填入t_ret
            for(auto& i: zi_map)
            {
                t_ret.push_back(i.second);
            }

        }
    }
    // 写入ret_ch
    for(int i = ret_ch.size() -1;i>=0;i--)
    {
        if(ret_ch[i] != 'a')
            continue;
        ret_ch[i] = t_ret.back();
        t_ret.pop_back();
    }
    // 开始打印
    for(auto& i: ret_ch)
    {
        cout << i;
    }
}













// 64 位输出请用 printf("%lld")
相关推荐
tingshuo29173 小时前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky5 天前
Django入门笔记
笔记·django
勇气要爆发5 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发5 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
qianshanxue115 天前
计算机操作的一些笔记标题
笔记
土拨鼠烧电路5 天前
笔记11:数据中台:不是数据仓库,是业务能力复用的引擎
数据仓库·笔记
土拨鼠烧电路5 天前
笔记14:集成与架构:连接孤岛,构建敏捷响应能力
笔记·架构
烟花落o5 天前
栈和队列的知识点及代码
开发语言·数据结构·笔记·栈和队列·编程学习