Google Code Jam Notes - All Your Base - Java

Problem:
Retrieved from:  https://code.google.com/codejam/contest/189252/dashboard#s=p0

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, TT 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 
 

Output 
 
3
11001001
cats
zig
Case #1: 201
Case #2: 75
Case #3: 11

Analysis:

The base is dependent on how many different characters in the string. The first digit has to be 1, and then we assign other digits to other different characters in the string. Use a HashMapM<Character, String> to record the relation between character and the assigned digit.

Time complexity: O(n), n is the length of the string.

My solution: (Your opinion is highly appreciated)


package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Zhenyi 2013 Dec 21, 2013 10:29:33 AM
 */
public class AllYourBase {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(
				"C:/Users/Zhenyi/Downloads/A-small-practice.in"));
		FileWriter out = new FileWriter(
				"C:/Users/Zhenyi/Downloads/A-small-practice.out");
		// BufferedReader in = new BufferedReader(new
		// FileReader("C:/Users/Zhenyi/Downloads/A-large-practice.in"));
		// FileWriter out = new
		// FileWriter("C:/Users/Zhenyi/Downloads/A-large-practice.out");

		int T = new Integer(in.readLine());

		for (int cases = 1; cases <= T; cases++) {
			char[] c = new String(in.readLine()).toCharArray();

			HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
			Integer[] a = new Integer[c.length];
			hm.put(c[0], 1);
			a[0] = 1;
			Integer num = 0;
			for (int i = 1; i < c.length; i++) {
				if (hm.containsKey(c[i])) {
					a[i] = hm.get(c[i]);
				} else {
					if (num == 1) {
						num++;
					}
					hm.put(c[i], num);
					a[i] = num;
					num++;
				}
			}

			int base = hm.size();
			if (base == 1)
				base = 2;

			long result = 0;

			for (int i = 0; i < c.length - 1; i++) {
				result = result * base + a[i] * base;
			}

			result = result + a[c.length - 1];

			out.write("Case #" + cases + ": " + result + "\n");
		}
		in.close();
		out.flush();
		out.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值