TopCoder Makes Me Crazy!

Arrgghhhh… JAVA + TOPCODER = CRAZY!

I did it twice! Yeah, I did a little-Javanase-bug for twice!

Firstly, in my SRM 390 competition, I did a typecasting of (int) which should be (long).

Secondly, in my Second Qual of TCO08, I just knew that we can’t assign an operation of character to a string variable in JAVA, we should assign a single character one after another.


import java.util.*;
public class ConcatenateNumber {
public int getSmallest(int number, int k) {
int res=1;
int dig=0;
int nn=number;
long n=number;
while (nn!=0){
dig++;
nn/=10;
}
boolean failed=false;
boolean[] result = new boolean[200010];
for (int i=0;i<k;i++) result[i]=false;
while (n%k!=0){
if (n/k==0){
res++;
n=n*(int)Math.pow(10,dig)+number;
n=n*(long)Math.pow(10,dig)+number;
}
else{
System.out.println(dig);
n%=k;
if (result[(int)n]){
failed=true;
break;
}
else result[(int)n]=true;
}
}
if (failed) res=-1;
return res;
}

————–

import java.util.*;
public class PhoneNumbers {
public static String coba;
public int cal2(int pos){
if (coba.charAt(pos)==coba.charAt(pos+1)) return 2;
else return 0;
}
public int cal3(int pos){
if ((coba.charAt(pos)==coba.charAt(pos+1))&&(coba.charAt(pos)==coba.charAt(pos+2))) return 2;
else if ((coba.charAt(pos)==coba.charAt(pos+1))||(coba.charAt(pos)==coba.charAt(pos+2))||(coba.charAt(pos+1)==coba.charAt(pos+2))) return 1;
return 0;
}
public String bestNumber(String number) {
String res=”";
coba = number;
int n = number.length();
if (n<=3) res=number;
else if (n==4) res=number.charAt(1)+number.charAt(2)+”-”+number.charAt(2)+number.charAt(3);
else if (n==4) res=”"+number.charAt(1)+number.charAt(2)+”-”+number.charAt(2)+number.charAt(3);
else{
int[] dp = new int[n+1];
int[] par = new int[n+1];
dp[n-2]=cal2(n-2);
dp[n-3]=cal3(n-3);
dp[n-4]=dp[n-2]+cal2(n-4);
par[n]=n+1;
par[n-2]=n;
par[n-3]=n;
par[n-4]=n-2;
for (int i=n-5;i>=0;i–){
dp[i]=dp[i+3]+cal3(i);
par[i]=i+3;
if (dp[i+2]+cal2(i)>=dp[i]){
dp[i]=dp[i+2]+cal2(i);
par[i]=i+2;
}
}
res=”";
int i=0;
while (par[i]<=n){
for (int j=i;j<par[i];j++) res+=coba.charAt(j);
i=par[i];
if (par[i]<=n) res+=’-';
}
}
return res;
}

February 10, 2008

4 responses to TopCoder Makes Me Crazy!

  1. Tenang aja, masih ada Qualification Round 3 koq, ayo tetap semangat brian :D .

    - Timo

  2. Ismail said:

    Wah , semangat ya Bri. Emang pake Java super ribet.
    Gw doain supaya TC lu beres deh.
    BTW lu pake applet apa bri ?
    Eh bang Timo handle TC nya apa?

    Good Luck

    Ismail_Afiff

  3. En Tay said:

    U’ll get use to it ;)

  4. @Ismail: handle saya “algoboy”.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>