题解 | 名字的漂亮度

名字的漂亮度

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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<char, int> mm;

int f(string s){
    vector<char> vec;
    //map<char, int> m;
    for(char c : s){
        if(mm.find(c) != mm.end()){
            mm[c]++;
            continue;
        }
        vec.push_back(c);
        mm[c] = 1;
    }
    auto lmda = [](char c1, char c2){return mm[c1] > mm[c2];};
    sort(vec.begin(), vec.end(), lmda);
    int t = 26;
    int sum = 0;
    for(char c : vec){
        sum += mm[c] * t;
        t--;
    }
    mm.clear();
    return sum;
    
}

int main() {
    int n;
    string s;
    cin >> n;
    while(n--){
        cin >> s;
        cout << f(s) << endl;

    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-11-25 09:53
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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