gpt4 book ai didi

java - UVa Online Judge on Erdos Number 运行时错误

转载 作者:行者123 更新时间:2023-12-02 07:27:01 24 4
gpt4 key购买 nike

我正在解决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);
}
}
}

最佳答案

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

您也不要使用任何内存技术并每次都构建相同的搜索树,这将导致 UVa Judge 出现Time Limit Exceeded 错误。

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

希望这有帮助

关于java - UVa Online Judge on Erdos Number 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com