题解 | #名字的漂亮度#
名字的漂亮度
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")