Skip to content

Instantly share code, notes, and snippets.

@bchetty
Created May 23, 2013 22:30
Show Gist options
  • Save bchetty/5639978 to your computer and use it in GitHub Desktop.
Save bchetty/5639978 to your computer and use it in GitHub Desktop.
All Your Base - GCJ 2009
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.math.BigInteger;
import java.io.*;
public class AllYourBase {
public BigInteger convert(String str) {
BigInteger res = new BigInteger("0");
int base = 0;
int len = str.length();
HashMap<String, Integer> hm = new HashMap<String, Integer>();
int index = 1;
for(int i=0;i<len;i++) {
if(!hm.containsKey("" + str.charAt(i))) {
if(i != 0) {
if(index == 1) {
index--;
} else if (index == 0) {
index = 2;
} else {
index++;
}
}
hm.put("" + str.charAt(i), new Integer(index));
base++;
}
}
if(hm.size() == 1) base = 2;
System.out.println("base : " + base + " Len : " + len);
for(int i=0;i<len;i++) {
String s = str.charAt(i) + "";
res = res.add(new BigInteger("" + hm.get(s)).multiply(new BigInteger("" + base).pow(len-i-1)));
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment