When John was a little kid he didn’t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could… he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string S, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn’t care about whether letters are
uppercase or lowercase, so that doesn’t affect the beauty of a letter. (Uppercase ‘F’ is exactly as beautiful as lowercase ‘f’, for example.)

You’re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?


The input file consists of a single integer m followed by m lines.

Your output should consist of, for each test case, a line containing the string “Case #x: y” where x is the case number (with 1 being the first case in the
input file, 2 being the second, etc.) and y is the maximum beauty for that test case.


5 <= M <= 50
2 <= length of S <= 500


    Good luck in the Facebook Hacker Cup this year!
    Ignore punctuation, please 🙂
    Sometimes test cases are hard to make up.
    So I just go consult Professor Dalves


    Case #1: 152
    Case #2: 754
    Case #3: 491
    Case #4: 729
    Case #5: 646


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

struct InvalidChar
    bool operator()(char c) const {
        return !isalpha((unsigned)c);

int main( int argc, char **argv) 
    if ( argc <= 1 ) {
        return 0;
    std::ifstream input(argv[1]);
    if ( input.peek() == EOF ) {
        return 0;
    // Read the number of test cases
    int M;
    input >> M ;
    // Read the first line and ignore it
    std::string line;
    std::getline (input,line);
    for ( int m = 0; m < M ; m++) {
        std::vector<int> occurences;
        std::getline (input,line);
        std::transform(line.begin(), line.end(), line.begin(), ::tolower);
        line.erase(std::remove_if(line.begin(),line.end(),InvalidChar()), line.end());
        if ( !line.empty()) {
            for ( int i = 0; i < (int)line.size(); i++) {
                occurences[line[i] - 'a']++;
        occurences.erase(std::remove(occurences.begin(), occurences.end(), 0), occurences.end());
        std::sort(occurences.begin(), occurences.end(), std::greater<int>());
        int multiply_factor = 26;
        int final_answer = 0;
        for ( std::vector<int>::iterator itr = occurences.begin(); itr != occurences.end(); ++itr) {
            final_answer += (*itr) * multiply_factor--;
        std::cout << "Case #" << m + 1 << ":  " << final_answer << std::endl;
    return 0;

5 thoughts on “BEAUTIFUL STRINGS

  1. “You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?”
    The example input is only an example. It’s not which of THOSE strings (in the example) is the most beautiful. The two clues are:
    1.) The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty.
    2.) Constraints: 5 ≤ m ≤ 50 / 2 ≤ length of s ≤ 500

    There’s the answer. Less than or equal to 500 characters in length and the highest beauty a letter can have is “26”. 500 x 26 = 13,000
    So the maximum beauty a string can have is 13,000.
    Example input ABbCcc = 152 or a + 2b + 3c = 152 (in this example C = 26)
    So the most beautiful string possible would be five hundred “C”s or –>

    • I still count 500 characters (one hundred per row plus five rows). Anyway, my point was that the maximum possible beauty (or highest value) of a string possible would be a total of thirteen thousand or 500 x 26. Am I missing something?

  2. That is not a valid case, you should have 500 characters in a single row. Another row would be next test case
    Try this, make sure you do line.size() after you read the line to check the characters in a string.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s