题解 | #名字的漂亮度#

名字的漂亮度

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

//使用hash表统计字符出现次数,再按照出现次数分类分数
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int N;
    cin>>N;
    for(int i=0; i<N; i++){
        string s;
        vector<int> hash(26, 0);
        cin>>s;
        int score = 26;
        int sum=0;
        for(int j=0;j<s.length();j++){
            hash[s[j]-'a']++;
        }

        for(int m=0;m<26;m++){
            for(int n=m+1;n<26;n++){
                if(hash[m]<hash[n]){
                    int temp;
                    temp=hash[m];
                    hash[m]=hash[n];
                    hash[n]=temp;
                }
            }
        }

        for(auto it:hash){
            if(it>0){
                sum+=it*score;
                score--;
            }
        }

        cout<<sum<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-16 15:04
黑龙江大学 Java
零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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