51nod 1182完美字符串

Input
輸入一個字符串S(S的長度 <= 10000),S中沒有除字母外的其他字符。
Output
由你將1-26分配給不同的字母,使得字符串S的完美度最大,輸出這個完美度。
Input示例
dad
Output示例
77<pre name="code" class="cpp">#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
char s[10005];
int b[500];
int cmp(int a,int b){
return a>b;
}
int main()
{
    int sum,len;
    while(scanf("%s",&s)!=EOF)
    {
        sum=0;
        memset(b,0,sizeof(b));
        len=strlen(s);
        for(int i = 0;i < len ;i++)
        {
            if(s[i]>='a'&&s[i]<='z'){
                b[s[i]]++;
            }
            else if(s[i]>='A'&&s[i]<='Z'){
                s[i]=s[i]-27;
                b[s[i]]++;
            }
        }
        int k = 26;
        sort(b,b+150,cmp);
        for(int i = 0; i < 30, k >= 1; ++i, k--)
        {
            if(b[i]!=0){
                sum=sum+b[i]*k;
            }

        }
        cout<<sum<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章