Programming challenge: character string perfection

Source: Internet
Author: User

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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.