All Your Base - GCJ 2009

First off, Welcome...and AYBABTU...I mean "All Your Base Are Belong To Us" :)

And now...lets get serious:

The problem listed below, is from the 'Round 1C' of Google Code Jam - 2009:

Problem
-------

In A.D. 2100, aliens came to Earth. They wrote a message in a cryptic language, and next to it they wrote a series of symbols. We've come to the conclusion that the symbols indicate a number: the number of seconds before war begins!

Unfortunately we have no idea what each symbol means. We've decided that each symbol indicates one digit, but we aren't sure what each digit means or what base the aliens are using. For example, if they wrote "ab2ac999", they could have meant "31536000" in base 10 -- exactly one year -- or they could have meant "12314555" in base 6 -- 398951 seconds, or about four and a half days. We are sure of three things: the number is positive; like us, the aliens will never start a number with a zero; and they aren't using unary (base 1).

Your job is to determine the minimum possible number of seconds before war begins.

Input
-----

The first line of input contains a single integer, T. T test cases follow. Each test case is a string on a line by itself. The line will contain only characters in the 'a' to 'z' and '0' to '9' ranges (with no spaces and no punctuation), representing the message the aliens left us. The test cases are independent, and can be in different bases with the symbols meaning different things.

Output
------

For each test case, output a line in the following format:

Case #X: V
Where X is the case number (starting from 1) and V is the minimum number of seconds before war begins.

Limits
------

1 ≤ T ≤ 100
The answer will never exceed 1018

Small dataset
-------------

1 ≤ the length of each line < 10 Large dataset ------------- 1 ≤ the length of each line < 61 Sample Input ------------ 3 11001001 cats zig Output ------- Case #1: 201 Case #2: 75 Case #3: 11





Coding the solution for the above problem, might sound easy, but its more trickier than what you first think. That is probably because you might understand it differently, than what the problem really wants.

The above problem statement, requires the output to be the minimum possible number. The first step is to understand that the number would be smaller if you take the smallest possible 'Base System' for the number. To find the base system that you want, you would have to find the number of unique symbols in the input string. If there are 2 'Unique' symbols in the string, take it as 'Base 2' system. If there are 10 unique symbols, take it as 'Base 10 (Decimal)' system.....and so on. Since the 'Unary System' is not used, ignore that.

After that, we need to find how we can make the sum, as minimal as possible. Since we don't know which digit stands for what, we have to make a decision for each symbol, so as to make the number, as small as possible. Since the left most digit carries more weight, this digit should be multiplied by the smallest digit. But since the left-most digit can't be zero (0), make it '1'. Use the Zero (0) for the second left-most digit (if there is 2nd digit in the number). Then use '2' for the third left most digit (if it is unique), '3' for the 4th left most digit (if it is unique)..and so on, unitl the end of the string with symbols. Here's the solution, that I coded in Java:

Interesting 'Google Suggestions'

Here are some interesting 'Google Suggestions' that you find, when you type some specific phrases : (It also shows the best of stereotypes that people have, in reality) :P

From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


From Google Suggestions


It's somehow funny, as well as serious at the same time, to see what people have on their minds. :P You can probably find many more interesting things like this, when you type some special phrases, on Google search box.

Alien Language - GCJ 2009

...Just finished the qualification round of 'Google Code Jam - 2009', passed through and just thought of posting this simple solution to the first problem, called 'Alien Language'. Here's the problem description:


Problem Statement
-----------------

After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.

Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.

A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.

Input
-----

The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.

Output
------

For each test case, output Case #X: K where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.

Limits
------

Small dataset
-------------

1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10

Large dataset
-------------

1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500

Sample Input
------------

3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc

Output
------

Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0


The solution to this problem is pretty simple, if you could figure it out, that its tailor-made for regular expressions. I used Java. So, I could create a solution, with just a couple of code statements. Here's the solution: