Question details
We need to assign each letter an integer between 1 and 26. The specific allocation is determined by you, but different letters have different degrees of perfection,
While the perfection of a string is equal to the sum of the perfection of all its letters, and does not care about uppercase and lowercase letters, that is, the perfection of letters F and f is the same.
Now we can output the maximum possible perfection of a string.
For example: dad, you can allocate 26 to d and 25 to a, so that the maximum possible perfection of the entire string is 77.
Function Header
C
Int perfect (const char * s );
C ++
Int perfect (const string & s );
Java
Public static int perfect (String s );
The first challenge failed and found to be a question-based problem, which requires the greatest perfection. I didn't take this into consideration! The Code has been modified. The source code is as follows:
<SPAN style = "FONT-SIZE: 12px"> # include <iostream> # include <string> using namespace std; int perfect (const string & s); int main () {while (true) {string str; cout <"Please enter the characters:"; cin> str; if (str = "0") break; int result = perfect (str); cout <"perfect result of" <result <endl;} return 0;} int perfect (const string & s) {int ia = (int) 'A'; // 97int iA = (int) 'A'; // 65int perfectNum = 0; string content = s; int config [26] = {0}; // obtain the number of records of characters while (content. size () {char ch = content [0]; while (true) {int index = content. find (content [0]); if (index <0) break; if (int) content [0] <ia) {// uppercase content config [(int) content [0]-iA] ++;} else {config [(int) content [0]-ia] ++;} content. erase (index, 1) ;}// sort the number of int I, j, t; for (I = 0; I <25; I ++) {for (j = 0; j <25-i; j ++) {if (config [j + 1]> config [j]) {t = config [j + 1]; config [j + 1] = config [j]; config [j] = t ;}}// start to calculate the maximum number of happiness (int I = 0, momey = 26; I <26; I ++, momey --) {perfectNum + = config [I] * momey;} return perfectNum;} </SPAN>