Wednesday, April 18, 2012

Google Code Jam 2012 - Qualification Round

Compared to my performance in 2011 Code Jam, this year I just turned out to be a big disaster.
Yes, and I found that its very hard to make my rusted brain to make it work on these awesome set of problems. Thank God the qualification cut-off was too low...so that Idiots like me also get into to Round-1 :P :P.

Problem 1 - Speaking in Tongues
I guess most of the people would have cracked it very easily and it was the same for me. Not much to think about the solution. Its just a Character Mapping from the input set. You will be able to map all 26-character very easily.


import java.util.Arrays;
import java.util.Scanner;

public class Problem1 {
public static void main(String args[]){
Scanner in= new Scanner(System.in);
String Normal="abcdefghijklmnopqrstuvwxyz ";
String Google="yhesocvxduiglbkrztnwjpfmaq ";
String fline=in.nextLine();
Scanner in1= new Scanner(fline);
int T = in1.nextInt();
for(int l=0;l<T;l++){
StringBuffer G=new StringBuffer(in.nextLine());
System.out.print("Case #"+(l+1)+": ");
for(int i=0;i<G.length();i++){
System.out.print(Google.charAt(Normal.indexOf(G.charAt(i))));
}
System.out.println();
}
}
}


Problem 3: Recycled Numbers
I thought it was a easy problem and I did solved the small input set. But the real problem was Large Input set and I made a big blunder in the computation.
I gave generic solution but never thought that the code would ditch me in large number range.After all i'm a bad coder. :P :P
This is what i've written for the small input set. There are better algorithms than what i've written here.
I could've avoided below silly mistakes. Anyways it saved me :D.
Didn't give much attention trailing 0's.
Computations take much time for Input range is large. I should've been little smart using the HashSet etc to get rid of duplicates.
I should've make use of the boundary conditions effectively.


import java.util.Scanner;

public class Problem3 {
public static void main(String args[]){
Scanner in= new Scanner(System.in);
Scanner rin= new Scanner(in.nextLine());
int T = rin.nextInt();
for(int l=0;l<T;l++){
rin= new Scanner(in.nextLine());
int lower = rin.nextInt();
int upper = rin.nextInt();
int count=0;
while(lower<upper){
for(int m=lower+1;m<=upper;m++){
if(isRecycledPair(lower,m)){
count++;
}
}
lower++;
}
System.out.println("Case #"+(l+1)+": "+count);
}

}

static String rightRotateString(String str){
char strC[]=str.toCharArray();
int i=0;
char temp=strC[strC.length-1];
for(i=strC.length-1;i>0;i--){
strC[i]=strC[i-1];
}
strC[i]=temp;
return (new String(strC));
}

static boolean isRecycledPair(int n,int m){
String nS=Integer.toString(n);
String mS=Integer.toString(m);
if(nS.matches(mS))
return true;
for(int i=0;i<nS.length()-1;i++){
nS=rightRotateString(nS);
if(nS.matches(mS)){
return true;
}
}
return false;
}
}


This is one of the work arounds for the same. The logic simple. Take a each number in the range. find all the rotations of that string :D . and the pair is definately a ReCycled pair for god sake :P :P. But you need to take care of the duplicates if there are any, so HashSet ;).


import java.util.HashSet;
import java.util.Scanner;

public class Problem3 {
public static void main(String args[]){
Scanner in= new Scanner(System.in);
Scanner rin= new Scanner(in.nextLine());
int T = rin.nextInt();
for(int t=1;t<=T;t++){
rin= new Scanner(in.nextLine());
System.out.println("Case #"+t+": "+getNumberOfPairs(rin.nextInt(),rin.nextInt()));
}
}

private static int getNumberOfPairs(int A,int B){
int count=0;
HashSet<Integer> s = new HashSet<Integer>();
for (int n = A; n < B; n++) {
String i=Integer.toString(n);
s.clear();
int t = i.length();
while(i.charAt(t-1)=='0')
t--;
for(int j=1;j<t;j++){
int m= Integer.parseInt(i.substring(j)+i.substring(0,j));
if(n<m && m<=B){
s.add(m);
}
}
count+=s.size();
}
return count;
}
}


Problem 2: Dancing with Googlers
This is one of the good problem. I guess, I was too lazy to think so I didn't try to solve this one in contest.
I missed one point while reading the problem, and I paid for that mistake in time :'(.

No triplet of scores contains scores that are more than 2 apart. - This one made my day like hell.

what are the possible sets.
Logic-1: If X is maximum then possible minimum will be X-2 in the triplet. Because possible triplets like this could be (X,X-2,X-2) or (X,X,X-2) or (X,X-1,X-2). Ofcourse they are surprising.
Logic-2: If X is maximum then possible minimum could be X-1 in the triplet.(Since Minimum X-1 is already ruled out in Logic-1). The possible triplets like this could be (X,X-1,X-1) or (X,X,X-1) - But these are not surprising Triplets
Logic-3: If X is maximum/minimum then possible triplet is (X,X,X) hehe Silly it not a Surprising Triplet.

So,
If a triplet is surprising then Maximum will be Average+ (1 or 2).
If a triplet is not surprising then Maximum will be Average+ (0 or 1).

If both are equal or more than P and maximum of surprising is equal or more than maximum of not surprising , then throw a count for it.
Simple it can be either surprising or not ....whats the big deal it has maximum than P in either case.
Save the urge to getting surprised for later, when you are sure that you will be more surprised with that triple.


import java.util.Scanner;

public class Problem2 {

private static int isMaxSurprising(int sum){
if(sum%3==0 && sum>0){
return (sum/3)+1;
}
return (sum/3)+(sum%3);
}

private static int isNotMaxSurprising(int sum){
if(sum%3==0 && sum>=0){
return (sum/3);
}
return (sum/3)+1;
}


public static void main(String args[]) throws Exception{
Scanner in= new Scanner(System.in);
Scanner rIn= new Scanner(in.nextLine());

int T=rIn.nextInt();

for(int t=1;t<=T;t++){
String nextLine=in.nextLine();
rIn = new Scanner(nextLine);
int N=rIn.nextInt();
int S=rIn.nextInt();
int p=rIn.nextInt();
int count=0;
for(int i=1;i<=N;i++){
int sum=rIn.nextInt();
int isMax=isMaxSurprising(sum);
int isMaxNot=isNotMaxSurprising(sum);
if(isMax >= isMaxNot && isMaxNot>=p){
count++;
}else if(S>0 && isMax >= isMaxNot && isMax >=p && isMaxNot <p){
count++;
S--;
}
}

System.out.println("Case #"+t+": "+count);
}
}
}


Problem D. Hall of Mirrors
I'm still thinking about it.:P