2012-11-18 58 views
0

我正在解決JAVA編程挑戰中的Erdos數問題。代碼在我的機器上完美運行。但是,在線評判會導致運行時錯誤。任何人都可以指出我犯的錯誤嗎?Erdos上的UVa Online Judge上的運行時錯誤號

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

關注一下。[1]。這裏提供的答案可能會解決您的問題。 [1]:http://stackoverflow.com/questions/6428569/runtime-error-in-uva-online-judge –

回答

1

除了NumberFormatException如果輸入不包含int其可以被產生,該程序也不會在一個很好的方法處理的輸入終止。

您還沒有使用任何記憶化技術,每一次,這將導致在弗吉尼亞法官Time Limit Exceeded錯誤構建相同的搜索樹。

您還應該使用緩衝輸入&輸出以進一步縮短編譯時間。減少對函數的調用,並在可能的情況下將它們內聯,即在同一範圍內編寫。

希望這有助於在此[鏈接]

1

一個問題可能是:

totalScenarios=Integer.parseInt(read()), 

這可能會引發NumberFormatException如果輸入的數值不int。你需要使用try/catch來處理它。

+0

這意味着由在線法官提供的輸入並不嚴格遵守規範提及。我們必須爲輸入提供單獨的錯誤檢查。 –