题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

python的方法首先想到的是哈希表
将单词的每个单词和出现次数用字典表示dict
然后进行值用sorted排序,注意排序的结果是一个数组,数组里面是元组形式
复制一个dict为dict_1用来表示字符和得分的键值对
首先创建一个数组nums 用来存储分数,分数就是26:26—len(temp)的数字,因为每个字符得分不一样
然后遍历将得分数组与dict1的值更新
最后用dict_1每个字符的得分和dict每个字符出现次数相乘就是答案
n=int(input())
while True:
    try:
        for i in range(n):
            str=input()
            dict={}
            for j in range(len(str)):
                if str[j] in dict.keys():
                    dict[str[j]]+=1
                else:
                    dict[str[j]]=1
            temp=sorted(dict.items(),key=lambda x:x[1],reverse=True)
            ans=[]
            nums=[]
            dict_1=dict.copy()
            for num in range(26,26-len(temp),-1):
                nums.append(num)
            for index in range(len(temp)):
                dict_1[temp[index][0]]=nums[index]
                ans.append(dict_1[temp[index][0]]*dict[temp[index][0]])
            print(sum(ans))   
    except:
        break


全部评论

相关推荐

07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 13:05
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务