思路(使用了from collections import Counter)
- 存一个{字符,出现次数}字典;
- 排序(使用value即可,我们不关心字符是什么,只关心出现次数),从大到小;
- 遍历values列表,与“26--”挨个相乘求和;
- 打印这个和。
代码
from collections import Counter while True: try: n = int(input()) while(n): n = n-1 s = input() counter = Counter(s) counter = sorted(counter.values(), reverse=True) start = 26 sum = 0 for i in counter: sum = sum+i*start start -= 1 print(sum) except: break