约束数组列表的所有组合

时间:2018-07-01 11:59:15

标签: java

我知道之前也曾问过类似的问题,但我发现答案令人困惑。我正在尝试制作一个程序,它将找到一个没有重复且只有最大大小的数组列表的每个组合。如果列表有4个项目,则应仅打印出所有4个项目均存在的组合。这是我到目前为止的内容:

public main(){
    UI.initialise();
    UI.addButton("Test", this::testCreate);
    UI.addButton("Quit", UI::quit);
}

public void createCombinations(ArrayList<String> list, String s, int depth) {
    if (depth == 0) {
        return;
    }

    depth --;

    for (int i = 0; i < list.size(); i++) {
        if (this.constrain(s + "_" + list.get(i), list.size())) {
            UI.println(s + "_" + list.get(i));
        }
        createCombinations(list, s + "_" + list.get(i), depth);
    }
}

public void testCreate() {
    ArrayList<String> n = new ArrayList<String>();
    n.add("A"); n.add("B"); n.add("C"); n.add("D");
    this.createCombinations(n , "", n.size());
}

public boolean constrain(String s, int size) {
    // Constrain to only the maximum length
    if ((s.length() != size*2)) {
        return false;
    }

    // Constrain to only combinations without repeats
    Scanner scan = new Scanner(s).useDelimiter("_");
    ArrayList<String> usedTokens = new ArrayList<String>();
    String token;

    while (scan.hasNext()) {
        token = scan.next();
        if (usedTokens.contains(token)) {
            return false;
        } else {
            usedTokens.add(token);
        }
    }

    // If we fully iterate over the loop then there are no repitions
    return true;
}

public static void main(String[] args){
    main obj = new main();
}   

这会打印出正确的以下内容:

_A_B_C_D
_A_B_D_C
_A_C_B_D
_A_C_D_B
_A_D_B_C
_A_D_C_B
_B_A_C_D
_B_A_D_C
_B_C_A_D
_B_C_D_A
_B_D_A_C
_B_D_C_A
_C_A_B_D
_C_A_D_B
_C_B_A_D
_C_B_D_A
_C_D_A_B
_C_D_B_A
_D_A_B_C
_D_A_C_B
_D_B_A_C
_D_B_C_A
_D_C_A_B
_D_C_B_A

这对于较小的列表有效,但对于较大的列表效率很低。我知道我所做的事情是完全错误的,但是我想学习正确的方法。任何帮助都非常感谢。提前致谢。

P.S。这不是家庭作业,只是出于兴趣,尽管我是一名新的CS学生(如果不是很明显)。

1 个答案:

答案 0 :(得分:0)

在Java中实现Heap's algorithm

import java.util.Arrays;

public class Main {
    public static void swap(final Object[] array, final int index1, final int index2) {
        final Object tmp = array[index1];
        array[index1] = array[index2];
        array[index2] = tmp;
    }

    public static void printPermutations_HeapsAlgorithm(final int n, final Object[] array) {
        final int[] c = new int[n];

        for (int i = 0; i < c.length; ++i)
            c[i] = 0;

        System.out.println(Arrays.toString(array)); //Consume first permutation.

        int i=0;

        while (i < n) {
            if (c[i] < i) {
                if ((i & 1) == 0)
                    swap(array, 0, i);
                else
                    swap(array, c[i], i);

                System.out.println(Arrays.toString(array)); //Consume permutation.

                ++c[i];
                i=0;
            }
            else
                c[i++] = 0;
        }
    }

    public static void main(final String[] args) {
        printPermutations_HeapsAlgorithm(4, new Character[]{'A', 'B', 'C', 'D'});
    }
}

可能是this的副本。

相关问题