字符串完美度---庞果编程挑战
#include <iostream> #include <string> #include <algorithm> #include <cctype> #include <cstring> #include <ctime> using namespace std; const int maxint=26; int hash1[26]; int perfect(const string &s) { memset(hash1,0,sizeof(hash1)); for(int i=0;i!=s.size();i++){ if(!isalpha(s[i]))continue; char tmp=tolower(s[i]); hash1[tmp-'a']++; } int sum=0; sort(hash1,hash1+26); int tmp=maxint; for(int i=25;i>=0;i--) { if(!hash1[i])continue; sum+=hash1[i]*tmp; tmp--; } return sum; } int main() { string s="dd//BCD..aa"; //clock_t start,end; //start=clock(); cout<<perfect(s)<<endl; //end=clock(); //cout<<end-start<<endl; return 0; }