HJ26 字符串排序

摘要
Title: HJ26 字符串排序
Tag: 字符串、stringstream
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

HJ26 字符串排序

  • 题意

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

  • 思路

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

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #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;
    }
使用搜索:谷歌必应百度