将List <List <String >>元素添加到List <List <List <String >>>是否不保留顺序? | Java中的排列和分区

时间:2019-09-16 15:32:44

标签: java algorithm list permutation partitioning

问题 我正在研究一种生成一组字符串的所有 ordered 分区的方法。 首先,生成所有分区,然后我要生成这些分区的所有排列。 例: {a,b,c}将会变成

[[a, b, c]]
[[b, c], [a]]
[[a, c], [b]]
[[c], [a, b]]
[[c], [b], [a]]

通过分区,然后进行排列:

[[a, b, c]]
[[b, c], [a]]
[[a], [b, c]]
[[a, c], [b]]
[[b], [a, c]]
[[c], [a, b]]
[[a, b], [c]]
[[c], [b], [a]]
[[b], [c], [a]]
[[a], [c], [b]]
[[c], [a], [b]]
[[b], [a], [c]]
[[a], [b], [c]]

当前实施 使用我的代码,我可以生成分区并确定应添加哪些排列(List<List<String>>)。 但是,将它们添加到排列的收集列表(List<List<List<String>>>)中会导致排列顺序丢失。由于这是排列的整体思想,因此很麻烦。 有没有办法在保留顺序的同时将此排列添加到列表中?这是我的代码:

        List<List<List<String>>> partitions;
        partitions = partitions(Arrays.asList(activities));

        permu = new ArrayList<>();

        for (List<List<String>> partition : partitions) {
            permutations(partition.size(), partition);
            System.out.println("Resulting list: " + permu);
        }
    private static List<List<List<String>>> partitions(List<String> inputSet) {
        List<List<List<String>>> res = new ArrayList<>();
        if (inputSet.isEmpty()) {
            List<List<String>> empty = new ArrayList<>();
            res.add(empty);
            return res;
        }
        int limit = 1 << (inputSet.size() - 1);
        // Note the separate variable to avoid resetting
        // the loop variable on each iteration.
        for (int j = 0; j < limit; ++j) {
            List<List<String>> parts = new ArrayList<>();
            List<String> part1 = new ArrayList<>();
            List<String> part2 = new ArrayList<>();
            parts.add(part1);
            parts.add(part2);
            int i = j;
            for (String item : inputSet) {
                parts.get(i&1).add(item);
                i >>= 1;
            }
            for (List<List<String>> b : partitions(part2)) {
                List<List<String>> holder = new ArrayList<>();
                holder.add(part1);
                holder.addAll(b);
                res.add(holder);
            }
        }
        return res;
    }
    //Heaps algorithm
    private void permutations(int k, List<List<String>> inputSet){
        if (k==1){
            permu.add(inputSet);
            System.out.println("Permutation added: "+inputSet); //This element still has order
        }
        else {
            // Generate permutations with kth unaltered
            // Initially k == length(A)
            permutations(k-1, inputSet);

            // Generate permutations for kth swapped with each k-1 initial
            for (int i = 0; i < k-1; i++ ){
                // Swap choice dependent on parity of k (even or odd)
                if (k%2 == 0){
                    swap(inputSet, i, k-1); // zero-indexed, the kth is at k-1 else
                }
                else {
                    swap(inputSet, 0, k-1);
                }
                permutations(k-1, inputSet);
            }
        }
    }

    //Helper method for PERMUTATIONS
    private void swap(List<List<String>> input, int a, int b) {
        List<String> temp = input.get(a);
        input.set(a, input.get(b));
        input.set(b, temp);
    }
}

当前结果 顶部和底部代码中的打印行在执行期间产生以下结果:

Permutation added: [[a, b, c]]
Resulting list: [[[a, b, c]]]
Permutation added: [[b, c], [a]]
Permutation added: [[a], [b, c]]
Resulting list: [[[a, b, c]], [[a], [b, c]], [[a], [b, c]]]
Permutation added: [[a, c], [b]]
Permutation added: [[b], [a, c]]
Resulting list: [[[a, b, c]], [[a], [b, c]], [[a], [b, c]], [[b], [a, c]], [[b], [a, c]]]
Permutation added: [[c], [a, b]]
Permutation added: [[a, b], [c]]
Resulting list: [[[a, b, c]], [[a], [b, c]], [[a], [b, c]], [[b], [a, c]], [[b], [a, c]], [[a, b], [c]], [[a, b], [c]]]
Permutation added: [[c], [b], [a]]
Permutation added: [[b], [c], [a]]
Permutation added: [[a], [c], [b]]
Permutation added: [[c], [a], [b]]
Permutation added: [[b], [a], [c]]
Permutation added: [[a], [b], [c]]
Resulting list: [[[a, b, c]], [[a], [b, c]], [[a], [b, c]], [[b], [a, c]], [[b], [a, c]], [[a, b], [c]], [[a, b], [c]], [[a], [b], [c]], [[a], [b], [c]], [[a], [b], [c]], [[a], [b], [c]], [[a], [b], [c]], [[a], [b], [c]]]

如您所见,尽管要添加的排列具有顺序,但在结果列表中丢失了。

0 个答案:

没有答案
相关问题