关于鄂尔多斯数的UVa在线判断的运行时错误

时间:2012-11-18 14:49:28

标签: java

我正在解决JAVA编程挑战中的鄂尔多斯数问题。 代码在我的机器中运行完美。但是在在线判断时会导致运行时错误。谁能指出我犯的错误?

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

这是代码

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

2 个答案:

答案 0 :(得分:1)

一个问题可能是:

totalScenarios=Integer.parseInt(read()),

如果输入的值不是NumberFormatException,则可能会抛出int。你需要使用try / catch来处理它。

答案 1 :(得分:1)

除了输入不包含NumberFormatException时可能生成的int之外,程序也不会以良好的方式处理输入终止。

您也不会使用任何记忆技术并且每次都建立相同的搜索树,这将导致UVa Judge出现Time Limit Exceeded错误。

你还应该使用输入和放大器的缓冲。输出以进一步减少编译时间。减少对函数的调用,如果可能,可以内联它们,即在同一范围内写入。

希望这有帮助