题解 | 名字的漂亮度

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	_, _ = fmt.Scan(&n)

	for i := 0; i < n; i++ {
		input := ""
		_, _ = fmt.Scan(&input)

		countMap := map[byte]int{}

		for _, c := range []byte(input) {
			if _, ok := countMap[c]; !ok {
				countMap[c] = 0
			}

			countMap[c]++
		}

		// fmt.Printf("countMap: %#v\n", countMap)

		countSlice := []int{}
		for _, v := range countMap {
			countSlice = append(countSlice, v)
		}

		sort.Slice(countSlice, func(i2, j int) bool {
			return countSlice[i2] > countSlice[j]
		})

		// fmt.Printf("countSlice2: %#v\n", countSlice)

		r := 0
		for idx, c := range countSlice {
			r += (26 - idx) * c
		}

		fmt.Printf("%v\n", r)
	}
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务