输入一个字符串S(S的长度 <= 10000),S中没有除字母外的其他字符。
由你将1-26分配给不同的字母,使得字符串S的完美度最大,输出这个完美度。
dad
77
水题
代码:
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char ch; int num[26]; char t[26]; int c,ans; bool cmp(char a,char b) { return num[a - 'a'] > num[b - 'a']; } int main() { while((ch = getchar()) != '\n') { if(num[tolower(ch) - 'a'] ++ == 0) t[c ++] = tolower(ch); } sort(t,t + c,cmp); for(int i = 0;i < c;i ++) { ans += num[t[i] - 'a'] * (26 - i); } printf("%d",ans); }