HJ26 字符串排序

发布于:2023-09-08 ⋅ 阅读:(59) ⋅ 点赞:(0)

Powered by:NEFU AB-IN

Link

HJ26 字符串排序

  • 题意

    编写一个程序,将输入字符串中的字符按如下规则排序。
    规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
    如,输入: Type 输出: epTy
    规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
    如,输入: BabA 输出: aABb
    规则 3 :非英文字母的其它字符保持原来的位置。
    如,输入: By?e 输出: Be?y

  • 思路

    先全放进map中,自动排序并保留顺序
    再全输入到流中,最后挨个输出出来

  • 代码

    #include <bits/stdc++.h>
    #include <cctype>
    #include <sstream>
    using namespace std;
    #define int long long
    #undef int
    
    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS                                                                                                            \
        ios::sync_with_stdio(false);                                                                                       \
        cin.tie(nullptr);                                                                                                  \
        cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    
    const int N = 1e5 + 10, INF = 0x3f3f3f3f;
    
    map <char, vector<char> > v;
    
    signed main() {
        //freopen("Tests/input_1.txt", "r", stdin);
        IOS;
    
        string s;
    
        getline(cin, s);
    
        for (auto i : s) {
            if (isalpha(i))
                v[tolower(i)].push_back(i);
        }
    
        stringstream ss;
    
        for (auto [i, vv] : v) {
            for (auto k : vv) ss << k;
        }
    
        string ans;
        ss >> ans;
        int id = 0;
        for (auto i : s) {
            if (isalpha(i))
                cout << ans[id ++];
            else cout << i;
        }
    
        return 0;
    }
    
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到