每次解题都要de好久的bug这次居然很快。
#include <iostream> #include <algorithm> #include <vector> using namespace std; void pld(string name) { cin>>name; int pl=0; int a[26]={0}; int m=26; vector<int> v ; for (int i=0;i<name.size();i++) { if (name[i]<='z'&&name[i]>='a') a[name[i]-'a']++; if (name[i]<='Z'&&name[i]>='A') a[name[i]-'A']++; } //sort(a,a+26); for (int i=0;i<26;i++) { if(a[i]>0) v.push_back(a[i]); } vector<int>::iterator it=v.begin(); sort(v.begin(), v.end(),greater<int>()); for(;it!=v.end();it++) { pl=pl+m*(*it); m--; //cout<<*it<<endl; } cout<<pl<<endl; } int main () { int N; cin>>N; string name; for (int i=0;i<N;i++) { pld(name);} }