题解 | #名字的漂亮度#

名字的漂亮度

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

#include <iostream>
using namespace std;
//类似哈希表的思想,记下字母的频率,让最多次数的字母为26,依次递减
int main() {
    int n;
    cin >> n;
    string s;
    while (cin >> s) {
        int result = 0;
        int beauty = 26;
        int hash[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            hash[s[i] - 'a']++;
        }
//插入排序
        for (int i = 1; i < 26; i++) {
            int key = hash[i];
            int j = i - 1;
            while (j >= 0 && hash[j] < key) {
                hash[j + 1] = hash[j];
                j--;
            }
            hash[j + 1] = key;
        }
        // //打印一下排完序的哈希表
        // for (int i = 0; i < s.size(); i++) {
        //     cout<<hash[i]<<" ";
        // }
        // cout<<endl;
        for (int i = 0; i < 26; i++) {
            result += hash[i] * beauty;
            beauty--;
        }
        cout << result << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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