题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 1. 先把每个字符出现的次数放在vector数组中
// 2. 对vector数组进行排序
// 3. 遍历数组,同时累加每个字符的漂亮权重,计算漂亮度
int main() {
    int n;
    cin >> n;
    string str;
    for(int i = 0; i < n; i++){
        cin >> str;
        vector<int> cnt(26, 0);
        for(auto s : str){
            cnt[s - 'a'] += 1;
        }

        sort(cnt.begin(), cnt.end());
        int start = 1;
        int beauty = 0;
        for(auto n : cnt){
            beauty += start*n;
            start++;
        }
        cout << beauty << endl;
    }
}
// 64 位输出请用 printf("%lld")

字符串操作 文章被收录于专栏

字符串操作

全部评论

相关推荐

ResourceUtilization:我嘞个董事长
点赞 评论 收藏
分享
嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务