0

I am solving the Erdos number problem from the programming challenges in JAVA. The code runs perfectly in my machine. However on the online judge it results in a runtime error. Could anyone point out the mistake i made?

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=985

Here is the code

import java.util.*;
import java.io.*;

class Main
{
private String inputLines[];
private String namesToBeFound[];
private String namesInEachBook[][];
private String searchItem;
private boolean solnfound=false;
private static final BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
static String read() throws IOException
{
    String line;
    while(true)
    {
        line=br.readLine();
        if(line==null) break; //eof
        else if(line.length()==0) continue; //blank line
        else
        {
            line=line.trim().replaceAll("\\s+"," ");
            return line;
        }
    }
    return null;
}

public static void main(String args[]) throws IOException
{
    Main ob=new Main();
    int totalPapers,calcAuthors,totalScenarios;
    //First input number of scenarios
    totalScenarios=Integer.parseInt(read());
    //Now start a loop for reading total number of scenarios
    for(int scenario=1;scenario<=totalScenarios;scenario++)
    {
        //Now read the line containing the number of papers and authors
        StringTokenizer line=new StringTokenizer(read()," ");
        totalPapers=Integer.parseInt(line.nextToken());
        calcAuthors=Integer.parseInt(line.nextToken());

        //Read a line containing author names along with book names
        ob.inputLines=new String[totalPapers];
        for(int i=0;i<totalPapers;i++)
            ob.inputLines[i]=read();

        //Read a line containing the names to be searched
        ob.namesToBeFound=new String[calcAuthors];
        for(int i=0;i<calcAuthors;i++)
            ob.namesToBeFound[i]=read();

        //Now generate the array
        ob.buildArray();
        //Now search
        System.out.println("Scenario "+scenario);
        for(int i=0;i<calcAuthors;i++)
        {
            ob.searchItem=ob.namesToBeFound[i];
            if(ob.searchItem.equals("Erdos, P."))
            {
                System.out.println("Erdos, P. 0");
                continue;
            }
            ob.search(ob.namesToBeFound[i],1,new ArrayList());
            if(ob.solnfound==false) System.out.println(ob.searchItem+" infinity");
            ob.solnfound=false;
        }
    }

}

private void buildArray()
{
    String str;
    namesInEachBook=new String[inputLines.length][];
    for(int i=0;i<inputLines.length;i++)
    {

        str=inputLines[i];
        str=str.substring(0,str.indexOf(':'));
        str+=",";
        namesInEachBook[i]=new String[(countCommas(str)+1)>>1];
        for(int j=0;j<namesInEachBook[i].length;j++)
        {
            str=str.trim();
            namesInEachBook[i][j]="";
            namesInEachBook[i][j]+=str.substring(0,str.indexOf(','))+",";
            str=str.substring(str.indexOf(',')+1);
            namesInEachBook[i][j]+=str.substring(0,str.indexOf(','));
            str=str.substring(str.indexOf(',')+1);
        }
    }
}

private int countCommas(String s)
{
    int num=0;
    for(int i=0;i<s.length();i++)
        if(s.charAt(i)==',') num++;
    return num;
}

private void search(String searchElem,int ernosDepth,ArrayList searchedElem)
{
    ArrayList searchSpace=new ArrayList();
    searchedElem.add(searchElem);
    for(int i=0;i<namesInEachBook.length;i++)

        for(int j=0;j<namesInEachBook[i].length;j++)
        {
            if(namesInEachBook[i][j].equals(searchElem))    //Add all authors name in this group
            {
                for(int k=0;k<namesInEachBook[i].length;k++)
                {
                    if(namesInEachBook[i][k].equals("Erdos, P.")) //Found
                    {
                        solnfound=true;
                        System.out.println(searchItem+" "+ernosDepth);
                        return;
                    }
                    else if(searchedElem.contains(namesInEachBook[i][k]) || searchSpace.contains(namesInEachBook[i][k])) continue;
                    searchSpace.add(namesInEachBook[i][k]);
                }
                break;
            }
        }
    Iterator i=searchSpace.iterator();
    while(i.hasNext())
    {
        String cSearchElem=(String)i.next();
        search(cSearchElem,ernosDepth+1,searchedElem);
    }
}
}
Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
  • Look at this [link][1]. The answer provided here may solve your problem. [1]: http://stackoverflow.com/questions/6428569/runtime-error-in-uva-online-judge – Alim Ul Gias Nov 18 '12 at 14:56

2 Answers2

1

One issue could be:

totalScenarios=Integer.parseInt(read()),

this may throw NumberFormatException if entered value is not int. You need to handle it using try/catch.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • That means the input provided by the online judge does not strictly adhere to the specifications mentioned. We have to provide a separate error checking for inputs. – Extreme Coders Nov 18 '12 at 15:02
1

Apart from NumberFormatException which may be generated if input does not contain an int, the program also does not handle termination of inputs in a good way.

You also do not use any memoization technique and building the same search tree every time, which will result in Time Limit Exceeded error on UVa Judge.

You should also use Buffering of both Input & Output for further reduction of Compilation times. Reduce calls to functions and if possible inline them that is, write within the same scope.

Hope this helps