https://github.com/qedsoftware/facebook-hackercup-2013-quals
(Python) Facebook Hackercup 2013 Qualifying Round solutions.
https://github.com/qedsoftware/facebook-hackercup-2013-quals
Last synced: 8 months ago
JSON representation
(Python) Facebook Hackercup 2013 Qualifying Round solutions.
- Host: GitHub
- URL: https://github.com/qedsoftware/facebook-hackercup-2013-quals
- Owner: qedsoftware
- Created: 2013-01-29T06:51:17.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2013-04-18T21:53:04.000Z (about 13 years ago)
- Last Synced: 2025-05-25T13:48:37.038Z (about 1 year ago)
- Language: Python
- Homepage:
- Size: 133 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
FACEBOOK-HACKERCUP-2013-QUALS
=============================
My solutions for Facebook Hackercup 2013, Qualifying Round.
William Wu
PROBLEM 1: BALANCED SMILEYS
=============================
When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs. 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 <= 500
__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
PROBLEM 2: BEAUTIFUL STRINGS
=============================
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 <= 100
__Example 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
PROBLEM 3: FIND THE MIN
=============================
After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.
John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is *not* contained in the previous *k* values of m.
For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.
John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.
Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).
Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:
m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k
__Input__
The first line contains an integer T (T <= 20), the number of test cases.
This is followed by T test cases, consisting of 2 lines each.
The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 105, k < n <= 109).
The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 109, 1 <= r <= 109).
__Output__
For each test case, output a single line containing the case number and the nth element of m.
__Example Input__
5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46
__Example Output__
Case #1: 8
Case #2: 38
Case #3: 41
Case #4: 40
Case #5: 12