Facebook Hacker Cup 2013 - Solutions
hi All,
This year's hacker cup was on my most busiest weekend, had office to come, friend's sister's marriage to attend, broken ankle and bruised knee accompanied by the urge to solve challenges it hacker cup. I wanted to give my 100%, but could only spare 3 hours and was able to solve only two problems. That too, I am not sure, when the solution is judged tomorrow, will it be right or wrong.
Problem 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 #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.
Constraints
5 ≤ m ≤ 50
2 ≤ length of s ≤ 500Example 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 DalvesExample Output:
Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646
My Solution:
Algorithm:
1. Clean the string and only allow characters a-z
2. make a frequency array for each character
3. apply beauty from 26 to all chars in decreasing order
4. Sum and return
Code:
Please feel free to test your input file here and lemme know if I am wrong:
Problem 2: Balanced Smileys
Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(
Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.
A message has balanced parentheses if it consists of one of the following:
- An empty string ""
- One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
- An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
- A message with balanced parentheses followed by another message with balanced parentheses.
- A smiley face ":)" or a frowny face ":("
Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.Input
The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases.
The following T lines each contain a message of length s that you got from John.Output
For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)Constraints
1 ≤ length of s ≤ 100Example Input:
5
:((
i am sick today (:()
(:)
hacker cup: started :):)
)(Example Output:
Case #1: NO
Case #2: YES
Case #3: YES
Case #4: YES
Case #5: NO
My Solution: IS WRONG!
The score board says my output is wrong, let's see what I did here wrong:
Algorithm:
1. if string empty, it is a balanced string YES
2. if it contains characters other than a-z :)(\s it is not a balanced string NO.
3. clean the string and leave only the parenthesis.
4. Match a generic Regex which helps us match all the complete brackets, as in:
5. delete the matches recursively and check if the remaining string is balanced or not
6. Output results
Code:
Please feel free to test your input file here and lemme know Where I went wrong:
I did not solve the 3rd problem, will do it in my free time, please update your thoughts on these solutions below.
Cheers!