2012-02-01 64 views
-1

我一直在研究UVa 630問題2天,http://acm.uva.es/p/v6/630.html 我已經寫了一個工作代碼,但是在提交後不斷得到錯誤的答案結果,我的程序對於給定的輸入工作正常格式並生成正確的結果,請有人看看我的代碼,並找出它有什麼問題。UVa 630不斷錯誤答案

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.Iterator; 

class Main { 

    String[] init; 
    String[] sorted; 
    int voca = 0; 
    String[] test; 
    private String x; 
    private String y; 
    ArrayList<ArrayList> initial = new ArrayList<ArrayList>(); 

    char[] charArray; 
    String input; 

    void initSort(String i) { 
     input = i; 
     charArray = input.toCharArray(); 
    } 

    char[] get() { 
     return charArray; 
    } 

    void sort(int low, int high) { 
     int i = low; 
     char pivot = charArray[low]; 
     for (int j = low + 1; j < high; j++) { 
      if (charArray[j] < pivot) { 
       if (j > i + 1) { 
        exchange(i + 1, j); 
       } 
       i++; 
      } 
     } 
     exchange(low, i); 
     if (i > low + 1) 
      sort(low, i); 
     if (i + 2 < high) 
      sort(i + 1, high); 
    } 

    void exchange(int i, int j) { 
     char temp = charArray[i]; 
     charArray[i] = charArray[j]; 
     charArray[j] = temp; 
    } 

    // ******************************************** 
    void begin() { 
     // System.out.println("begin test"); 
     int numOfdataSet; 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     try { 
      numOfdataSet = Integer.parseInt(in.readLine()); 
      do { 
       ArrayList currentSet = new ArrayList(); 

       int numberOfdic; 
       String tempStringV; 
       String tempStringT; 
       int i = 0; 
       try { 

        String emptyLine = in.readLine(); 
        numberOfdic = Integer.parseInt(in.readLine()); 
        ; 
        currentSet.add(emptyLine); 
        currentSet.add(Integer.toString(numberOfdic)); 
        // System.out.println("enter numberOfdic is " + 
        // numberOfdic); 
        while (i < numberOfdic) { 
         tempStringV = in.readLine(); 
         currentSet.add(tempStringV); 
         i++; 
        } 
        // System.out.println("input test word"); 
        tempStringT = in.readLine(); 
        currentSet.add(tempStringT); 
        while (!tempStringT.equals("END")) { 

         tempStringT = in.readLine(); 
         currentSet.add(tempStringT); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       initial.add(currentSet); 

       numOfdataSet--; 

      } while (numOfdataSet != 0); 
     } catch (IOException e) { 
     } 
     ; 
     print(); 
    } 

    // *************************************************** 

    void getArrays(ArrayList<String> a) { 
     // read the file and put the words into a temporary array 
     String[] temp = new String[a.size()]; 
     for (int i = 0; i < a.size(); i++) { 
      temp[i] = a.get(i); 
      // System.out.println("temp "+i+" is "+temp[i]); 
     } 

     // extract the vocabulary counter from the temp array 
     int vocaCounter; 
     int shift; 
     if (!temp[0].equals("")) { 
      shift = 2; 
      vocaCounter = Integer.parseInt(temp[1]); 
     } else { 
      shift = 2; 
      vocaCounter = Integer.parseInt(temp[1]); 
     } 
     // System.out.println("there are "+vocaCounter); 

     // store the vocabulary into the array named as init 
     init = new String[vocaCounter]; 
     for (int i = shift; i < vocaCounter + shift; i++) { 
      init[i - shift] = temp[i]; 
      // System.out.println(i - shift + " voca is " + init[i - shift]); 
     } 
     // store the test words into the array named as test 
     test = new String[temp.length - vocaCounter - shift - 1]; 
     for (int j = 0; j < temp.length - vocaCounter - shift - 1; j++) { 

      test[j] = temp[j + vocaCounter + shift]; 
      // System.out.println("test "+j+" is "+test[j]); 
     } 
     sorted = init; 

    } 

    /** 
    * sort the two strings 
    */ 
    void arraySorter() { 
     x = x.toLowerCase(); 
     initSort(x); 
     sort(0, x.length()); 
     get(); 
     // java.util.Arrays.sort(FirstArray); 
     y = y.toLowerCase(); 
     initSort(y); 
     sort(0, y.length()); 
     get(); 
    } 

    String getEle(String in) { 
     initSort(in); 
     sort(0, in.length()); 
     return new String(get()); 
    } 

    void print() { 
     Iterator<ArrayList> iterator = initial.iterator(); 
     while (iterator.hasNext()) { 
      getArrays((ArrayList<String>) iterator.next()); 
      // **************** 
      /** 
      * sort the test array and store it as the testSort array 
      */ 
      String[] testSort = new String[test.length]; 
      for (int i = 0; i < test.length; i++) { 
       testSort[i] = test[i]; 
      } 
      for (int i = 0; i < test.length; i++) { 
       testSort[i] = getEle(testSort[i]); 
      } 
      // for(int i=0;i<test.length;i++) 
      // { 
      // System.out.println("test is "+test[i]+" and test sorted is "+testSort[i]); 
      // } 

      /** 
      * sort the vocabulary array and store the sorted array as vocaSort 
      */ 
      String[] vocaSort = new String[init.length]; 
      for (int i = 0; i < init.length; i++) { 
       vocaSort[i] = init[i]; 
      } 
      for (int i = 0; i < init.length; i++) { 
       vocaSort[i] = getEle(vocaSort[i]); 
      } 

      // start the testing process 
      for (int i = 0; i < test.length; i++) { 

       int counter = 1; 

       System.out.println("Anagrams for: " + test[i]); 
       for (int j = 0; j < sorted.length; j++) { 
        // anagramTester(test[i], init[j]); 

        boolean result = testSort[i].equals(vocaSort[j]);// //AnagramTester(); 
        if (result == true && counter < 10) { 
         System.out.println(" " + counter + ") " + init[j]); 

         counter++; 
        } else if (result == true && counter < 100) { 
         System.out.println(" " + counter + ") " + init[j]); 

         counter++; 
        } else if (result == true && counter < 1000) { 
         System.out.println("" + counter + ") " + init[j]); 

         counter++; 
        } 
       } 
       if (counter == 1) 
        System.out.println("No anagrams for: " + test[i]); 

      } 
      System.out.println(); 
     } 
    } 

    /** 
    * main function 
    * 
    * @param args 
    */ 
    public static void main(String[] args) { 
     Main myWork = new Main(); // create a dinamic instance 
     myWork.begin(); 

    } 

} 
下面

是input.txt的文件

1 

    4 
    atol 
    lato 
    rola 
    tara 
    kola 
    tola 
    END 

    2 

    24 
    uhgj 
    uhjg 
    ughj 
    ugjh 
    ujhg 
    ujgh 
    hujg 
    hugj 
    hgju 
    hguj 
    hjgu 
    hjug 
    guhj 
    gujh 
    ghuj 
    ghju 
    gjuh 
    gjhu 
    jugh 
    juhg 
    jhgu 
    jhug 
    jghu 
    jguh 
    jguh 
    END 
+0

如果你給的程序試試,你會發現它產生完全正確的結果,因爲它的預期在630的問題,我沒有看到任何錯誤的內部算法,因爲我正確地使用快速排序首先對數組進行排序並比較他們 – starcaller 2012-02-01 15:09:10

+0

這將大大簡化代碼,如果你切換到使用「陣列.sort'或'Collections.sort'。目前,很難看到樹木的樹林。 – 2012-02-01 17:00:22

+0

@JeffFoster是啊我同意這一點,但是,我真的想實施自己的排序方法,btw感謝您的回覆 – starcaller 2012-02-01 17:39:42

回答

0

這是一個輸出格式問題,問題就解決了

+0

請標記爲正確的答案然後 – Luciano 2012-02-02 01:42:11