题解 | #名字的漂亮度#

名字的漂亮度

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

bool compare(const pair<char, int>& a, const pair<char, int>& b) {
    return a.second > b.second;
    }
int beauty(string s) {  
    int sum = 0, value = 26;    
    map<char, int> mp;
    for (char i : s) {
        mp[i] += 1;
    }
    vector<pair<char, int>> vec(mp.begin(), mp.end());    
    sort(vec.begin(), vec.end(), compare);   
    for (auto v : vec) {
        if (vec.size() != 0) {              
        sum += value * v.second;
        value--;         
        }            
    }
    return sum;
}

int main() {
    int n;
    vector<string> str;
    while (cin >> n) {  
        str.resize(n);      
        for (int i = 0; i < n; i++) {
            cin >> str[i];
        }
        for (auto i : str) {
            int res = beauty(i);
            cout << res << endl;
        }
            
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

12-27 22:49
门头沟学院 Java
点赞 评论 收藏
分享
ddzd:比较说第一个项目,ai对话不要仅仅是掉api,能不能结合实验室数据封装一两个定制组件?有没有考虑ai回答的边界case和安全情况?可不可能设计一个momery来存储每个人的偏好和使用习惯?性能优化具体优化了多少呢?为什么选这个优化指标呢?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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