Runtime error on UVa Online Judge on Erdos Number

Posted by 2012 - End of the World on Stack Overflow See other posts from Stack Overflow or by 2012 - End of the World
Published on 2012-11-18T14:49:28Z Indexed on 2012/12/04 5:04 UTC
Read the original article Hit count: 230

Filed under:

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);
    }
}
}

© Stack Overflow or respective owner

Related posts about java