import sys
import string
all_ac = string.ascii_letters
for line in sys.stdin:
    a = line.split("\n")[0]
    kongge = {}
    res = ""
    line_ord = []
    big = {}
    count = {}
    for index,_ in enumerate(a):
        _ord = ord(_.lower())
        if _ord in count:
            count[_ord] += 1
        else:
            count[_ord] = 0
        if _ not in all_ac:
            kongge[index]=_
            continue
        else:
            if _.lower() != _:
                if _ord not in big:
                    big[_ord]=set()
                big[_ord].add(count[_ord])
            line_ord.append(_ord)
        last = _
    line_ord = sorted(line_ord)
    line_ord_index = 0
    #print(big)
    count = {}
    for i in range(len(a)):
        if i in kongge:
            res+=kongge[i]
            continue
        temp = line_ord[line_ord_index]
        if temp in count:
            count[temp] += 1
        else:
            count[temp] = 0
        if temp in big:
            big_list = big[temp]
            if count[temp] in big_list:
                res+=chr(line_ord[line_ord_index]).upper()
                big[temp].remove(count[temp])
            else:
                res+=chr(line_ord[line_ord_index])
        else:
            res+=chr(line_ord[line_ord_index])
        line_ord_index+=1
    print(res)
    

搞3个hash,一个统计字符出现了几次,一个统计低N个为特殊字符,一个用于记录低N个字符需要升为大写

然后两次循环即可