1 solutions

  • 0
    @ 2026-10-22 18:42:29

    C :

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char s[51];
        scanf("%s", s);
        int len = strlen(s);
        int char_num[26] = { 0 };
        for (int i = 0; i < len; i++) {
            char_num[s[i] - 'a']++;
        }
        char res[51] = { 0 };
        int res_pos = 0;
        while (res_pos < len) {
            char last_add = 0;
            while (1) {
                char now = -1;
                for (int i = 0; i < 26; i++) {
                    if (char_num[i] > 0 && (i + 'a') > last_add) {
                        now = i + 'a';
                        break;
                    }
                }
                if (now == -1) {
                    break;
                }
                res[res_pos] = now;
                res_pos++;
                char_num[now - 'a']--;
                last_add = now;
            }
            last_add = 'z' + 1;
            while (1) {
                char now = -1;
                for (int i = 25; i >= 0; i--) {
                    if (char_num[i] > 0 && (i + 'a') < last_add) {
                        now = i + 'a';
                        break;
                    }
                }
                if (now == -1) {
                    break;
                }
                res[res_pos] = now;
                res_pos++;
                char_num[now - 'a']--;
                last_add = now;
            }
        }
        printf("%s\n", res);
        return 0;
    }
    

    C++ :

    undefined
    

    Python :

    undefined
    
    • 1

    Information

    ID
    385
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By