UVa 630一直得到错误的答案

时间:2012-02-01 15:00:37

标签: java sorting

我一直在研究UVa 630问题2天,http://acm.uva.es/p/v6/630.html 我已经编写了一个工作代码,但是在提交后不断得到错误的答案结果,我的程序在给定的输入格式下工作正常并生成正确的结果,请有人请看一下我的代码并找出它有什么问题。< / p>

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

1 个答案:

答案 0 :(得分:0)

这是输出格式问题,问题已解决