确定阵列中各种字符串组合的各种组合

问题描述 投票:0回答:1
在lua中,我想获得整个字符串中各种可能组合的所有可能组合...让我解释一下我的意思。 假设我们有一个数组C = {“ A”,“ B”,“ C”,“ D”,“ E”}。 我想得到这个结果:

output = { { "a", "b", "c", "d", "e"}; { "ab", "c", "d", "e}; { "ac", "b", "d", "e" }; { "abc", "d", "e"}; { "aeb", "d", "c"}; { "a", "bde", "c"}; { "ab", "cd", "e"}; { "a", "bcd", "e" }; { "acd", "b", "e}; { "ac", "bd", "e"}; { "abc", "de"}; { "abcd", "e"}; { "acd", "be"} { "ac" "bde"}; ...and so on until {"abcde"}; }
So every WHOLE string from C array is combine with every other whole string - and the higher combinations of this combinations combine with every other - and so on - giving ONLY UNIQUE combinations without repeating any element - which differs it from "superpermutation" because any combo must be "first unique"[so to speak] - so "ab" or "abc", but no "ba", "aba", "bab", "bac" etc. .

老实说,我无法掌握这个问题。也许有人知道如何解决这个问题?

到目前为止,我尝试的是通过以下功能获得C数组的基本排列:

local function getPermuations(array) local wrap, yield, board = coroutine.wrap, coroutine.yield, {}; local function append (t, new) local clone = {} for _, item in ipairs (t) do clone [#clone + 1] = item end clone [#clone + 1] = new return clone end local function permutate (tbl, sub, min) sub = sub or {} min = min or 1 return wrap (function () if #sub > lim then yield (sub); end if #sub < #tbl then for i = min, #tbl do for combo in permutate (tbl, append (sub, tbl [i]), i + 1) do yield (combo) end end end end) end for combo in permutate(array) do --table.sort( combo, self.sortAlphabetical ) table.insert(board, combo); end return board; end

返回阵列表,每个阵列都有一个置换。之后,我尝试编写一个复发函数,该功能将每个返回的数组从C中取出许多元素,并将其定位等等。因此,基本上是“超级重复置换”之类的东西。但是我无法做到这一点:
local function getRest(str, inh)
    local backbone, rest = {}, {};
    for k,v in ipairs(C) do
        backbone[v] = true;
    end
        for key in pairs(inh) do
            for k,v in ipairs(C) do
                if string.find(key, v) then backbone[v] = nil; end
            end;
        end;
        for _, e in ipairs(C) do
            if string.find(str, e) then backbone[e] = nil; end
        end
        if next(backbone) then
            for obj in pairs(backbone) do
                table.insert(rest, obj)
            end
            table.sort(rest, function(a, b)
            return string.lower(a) < string.lower(b)
        end)
    end     
    return rest;
end;
local mainBoard = {};
local function superpermutation(permutation, inheritance)
    inheritance = inheritance or {};
    local px = getPermuations(permutation);
    for _, array in ipairs(px) do
        local r = getRest(table.concat(array), inheritance);
        if #r>1 then
            inheritance[table.concat(array)] = true;
            superpermutation(r, inheritance);
        elseif next(inheritance) then
            local m = {};
            for k in pairs(inheritance) do
                    table.insert(m, {k})
            end;
            if r and r[1] then
                table.insert(m, {r[1]})
            end
            table.insert(mainBoard, m)
        end;
    end
    return
end;
superpermutation(C);
for index, superarray in ipairs(mainBoard) do
    local str = "";
    for _, array in ipairs(superarray) do
        str = str.."("..table.concat( array )..")"
    end
    print(str)
end

可以通过对算法进行分区来解决您的问题,例如center oferset
的所有分区。 解决方案是通过使用递归来生成给定集
algorithm lua permutation combinatorics
1个回答
0
投票
的所有可能分区来实现的。这是

lua实施: -- Function to generate all partitions of a set local function generatePartitions(input, index, currentPartition, result) if index == #input then -- Convert the current partition to the desired format local partition = {} for _, group in ipairs(currentPartition) do table.insert(partition, table.concat(group, "")) end table.insert(result, partition) else -- Add the current element to each existing group in the partition for i = 1, #currentPartition do table.insert(currentPartition[i], input[index + 1]) generatePartitions(input, index + 1, currentPartition, result) table.remove(currentPartition[i]) -- backtrack end -- Create a new group with the current element local newGroup = {input[index + 1]} table.insert(currentPartition, newGroup) generatePartitions(input, index + 1, currentPartition, result) table.remove(currentPartition) -- backtrack end end local input = {"a", "b", "c", "d", "e"} local results = {} generatePartitions(input, 0, {}, results) -- Print all results for _, result in ipairs(results) do print("{" .. table.concat(result, ", ") .. "}") end there是您特定集C的输出C: {abcde} {abcd, e} {abce, d} {abc, de} {abc, d, e} {abde, c} {abd, ce} {abd, c, e} {abe, cd} {ab, cde} {ab, cd, e} {abe, c, d} {ab, ce, d} {ab, c, de} {ab, c, d, e} {acde, b} {acd, be} {acd, b, e} {ace, bd} {ac, bde} {ac, bd, e} {ace, b, d} {ac, be, d} {ac, b, de} {ac, b, d, e} {ade, bc} {ad, bce} {ad, bc, e} {ae, bcd} {a, bcde} {a, bcd, e} {ae, bc, d} {a, bce, d} {a, bc, de} {a, bc, d, e} {ade, b, c} {ad, be, c} {ad, b, ce} {ad, b, c, e} {ae, bd, c} {a, bde, c} {a, bd, ce} {a, bd, c, e} {ae, b, cd} {a, be, cd} {a, b, cde} {a, b, cd, e} {ae, b, c, d} {a, be, c, d} {a, b, ce, d} {a, b, c, de} {a, b, c, d, e}

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.