DP problem

14 views
Skip to first unread message

Abhishek Khanna

unread,
Mar 12, 2011, 3:41:38 PM3/12/11
to Coders in NIT Durgapur
i tried a simple DP problem of codechef (link: http://www.codechef.com/problems/SUMTRIAN).
I am getting correct results on my system but on submission i get
Runtime Error (NZEC).Plzz help.My code is as follows:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
*
* @author abhishek khanna
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(new
OutputStreamWriter(System.out));
short test=Short.parseShort(br.readLine());
short i;
String[] str=new String[100];

for(i=0;i<test;i++)
{
byte rows=Byte.parseByte(br.readLine());
byte capacity=(byte)(rows+1);
byte[][] arr=new byte[capacity][capacity];
short[][] sum=new short[capacity][capacity];
short totalsum,maxsum;
byte r,c;
byte first=Byte.parseByte(br.readLine());
arr[1][1]=first;
sum[1][1]=first;
totalsum=first;
for(r=2;r<=rows;r++)
{
str=br.readLine().split(" ");
for(c=1;c<=r;c++)
{
if(c==1)
{
maxsum=sum[r-1][1];
}
else if(c==r)
{
maxsum=sum[r-1][r-1];
}
else
{
if(sum[r-1][c]>sum[r-1][c-1])
{
maxsum=sum[r-1][c];
}
else
{
maxsum=sum[r-1][c-1];
}
}
arr[r][c]=Byte.parseByte(str[c-1]);
sum[r][c]=(short) (arr[r][c] + maxsum);
if(totalsum<sum[r][c])
{
totalsum=sum[r][c];

}
}

}
pw.println(totalsum);
}
pw.flush();
// TODO code application logic here
}

}

Shalini Sah

unread,
Mar 14, 2011, 3:43:16 AM3/14/11
to nitdc...@googlegroups.com
Your code gives NumberFormatException
Use Scanner class instead of BufferedReader

ANUJ KUMAR

unread,
Mar 14, 2011, 3:58:26 AM3/14/11
to nitdc...@googlegroups.com
if u still have some problem i can give u my ac code. it was pretty
easy i got ac in 1 time :)

abhishek khanna

unread,
Mar 14, 2011, 1:14:26 PM3/14/11
to nitdc...@googlegroups.com
when i used trim() to remove extra spaces and also used "//s+" instead of " " as the delimiter with BufferedReader to accomodate any no of white spaces then it works.Scanner should be avoided as it is far too slow as compared to BufferedReader.

vineet gandhi

unread,
Mar 16, 2011, 10:22:29 AM3/16/11
to nitdc...@googlegroups.com
kya baat he khanna ji!
Reply all
Reply to author
Forward
0 new messages