0

我正在从 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);
    }
}
}
4

2 回答 2

1

一个问题可能是:

totalScenarios=Integer.parseInt(read()),

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

于 2012-11-18T14:52:00.473 回答
1

除此之外,NumberFormatException如果输入不包含int,则可能会生成,程序也不能很好地处理输入的终止。

您也没有使用任何记忆技术并且每次都构建相同的搜索树,这将导致Time Limit ExceededUVa Judge 出错。

您还应该使用输入和输出的缓冲来进一步减少编译时间。减少对函数的调用,并尽可能内联它们,即在同一范围内编写。

希望这可以帮助

于 2012-12-04T05:02:24.753 回答