题解 | #HJ45名字的漂亮度#

名字的漂亮度

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

#include <bits/stdc++.h>
#include<cctype>
using namespace std;
struct node {
    char name;
    int times = 0;
};
bool cmp(node a, node b) {
    return a.times > b.times;
}
int main() {
    int n;
    cin >> n;
    while (n--) {
        vector<node> v(26);
        vector<int> value(26);
        string s;
        cin >> s;
        for (int i = 0; i < s.size(); i++) {
            if (isupper(s[i])) s[i] += 32;
            v[s[i] - 'a'].name = s[i];
            v[s[i] - 'a'].times += 1;
        }
        sort(v.begin(), v.end(), cmp);
        int temp = 26;
        for (int i = 0; i < v.size(); i++) {
            if (v[i].times > 0) value[v[i].name - 'a'] = temp--;
        }
        int ans = 0;
        for (int i = 0; i < s.size(); i++) ans += value[s[i] - 'a'];
        cout << ans << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

ldyllic:飞神,985+美团+腾讯+京东,无敌飞飞神
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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