Facebook Hacker Cup 2013 Qualifying round

Facebooks yearly Hacker Cup started last weekend with its qualifying round running through the weekend… 3 questions, nothing TOO complicated, solve one and you’re in for round 1 which is February 2nd. I managed to squeeze my way into the 1st round by solving only the 1st question… unfortunately I didn’t read the instructions and downloaded the test file for the 2nd question which then triggered a timer of 6 minutes to submit the source code and answers… and I had neither… The third question was a pain, didn’t bother with it too much (2 tests to study for). So here’s the 1st question AND my code for it… I actually ranked 6915 out of 10169… lol.

Question 1: Beautiful Strings

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?

Input

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

Output

Your output should consist of, for each test case, a line containing the string “Case #xy” 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.

Example Input:

5
ABbCcc
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

Example Output:

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

My solution… again, can be cleaned up, save a couple of lines… but meh.

import java.util.Scanner;

public class BeautifulStrings {

    public static void main(String args[]) {
        Scanner scn = new Scanner(System.in);
        final int[] chars = new int[26];
        int tempMax = 0;
        int tempMaxIndex = 0;
        int finalAnswer = 0;
        final int cases = Integer.parseInt(scn.nextLine());
        for (int i = 0; i <= cases; i++) {
            int HighestValueAvailable = 26;
            final String tempS = scn.nextLine().toLowerCase(); //temporary string converted into lower case only
            final char[] temp = tempS.toCharArray();
            for (int j = 0; j < tempS.length(); j++) {
                final int currentChar = (int) temp[j];
                if (currentChar >= 97 && currentChar <= 122) { // ensure the characters are letters.
                    chars[currentChar - 97]++;
                }
            }
            for (int a = 0; a < tempS.length(); a++) {
                for (int b = 0; b < chars.length; b++) {
                    if (chars[b] > tempMax) { // find the number that re-appears the most.
                        tempMax = chars[b]; //keep the value, and index of the value.
                        tempMaxIndex = b;
                    }
                }
                finalAnswer = finalAnswer + (HighestValueAvailable * tempMax); // calculate answer and restart variables.
                chars[tempMaxIndex] = 0;
                HighestValueAvailable--;
                tempMax = 0;
            }
            System.out.printf("Case #%d: %dn", i + 1, finalAnswer);
            finalAnswer = 0;
        }
    }
}

									

Leave a Reply

Your email address will not be published. Required fields are marked *