How can I determine all possible ways a subsequence can be removed from a sequence?

前端 未结 6 2057
执念已碎
执念已碎 2021-01-31 08:29

Given two sequences, A and B, how can I generate a list of all the possible ways that B can be removed from A?

For example, In JavaSc

6条回答
  •  盖世英雄少女心
    2021-01-31 08:56

    The algorithm:

    1. Recursively build a tree of nodes, starting from the first element in B. Each node's value is the index of the subsequence item matching its level and its descendants are the indices of the next item -- so for [1,2,1,3,1,4,4], [1,4,4] the tree would be [ [ 0, [5, [6]], [6] ], [ 2, [5, [6]], [6] ], [ 4, [5, [6]], [6] ].
    2. Walk this tree and build up subsequences of items to delete, i.e. find all paths in the tree that are as long as the subsequence. This would result in a list like [ [ 0, 5, 6 ], [ 2, 5, 6 ], [ 4, 5, 6 ] ].
    3. For each list thus developed, append the list that results from the elements at those indices being deleted: [ [ 2, 1, 3, 1 ], [ 1, 2, 3, 1 ], [ 1, 2, 1, 3 ] ].

    The code to do this, which matches all your test cases:


    #!/usr/bin/env node
    
    var _findSubSeqs = function(outer, inner, current) {
    
        var results = [];
        for (var oi = current; oi < outer.length; oi++) {
            if (outer[oi] == inner[0]) {
                var node = {
                    value: oi,
                    children: _findSubSeqs(outer, inner.slice(1), oi+1)
                };
                results.push(node);
                }
        }
        return results;
    }
    
    var findSubSeqs = function(outer, inner) {
        var results = _findSubSeqs(outer, inner, 0);
        return walkTree(results).filter(function(a) {return (a.length == inner.length)});
    }
    
    var _walkTree = function(node) {
        var results = [];
        if (node.children.length) {
            for (var n = 0; n < node.children.length; n++) {
                var res = _walkTree(node.children[n])
                for (r of res) {
                    results.push([node.value].concat(r))
                }
            }
        } else {
            return [[node.value]]
        }
        return results
    }
    
    var walkTree = function(nds) {
        var results = [];
        for (var i = 0; i < nds.length; i++) {
            results = results.concat(_walkTree(nds[i]))
        }
        return results
    }
    
    var removeSubSeq = function(outer, inner) {
        var res = findSubSeqs(outer, inner);
        var subs = [];
        for (r of res) {
            var s = [];
            var k = 0;
            for (var i = 0; i < outer.length; i++) {
                if (i == r[k]) {
                    k++;
                } else {
                    s.push(outer[i]);
                }
            }
            subs.push(s);
        }
        return subs
    }
    
    console.log(removeSubSeq([1,2,1,3,1,4,4], [1,4,4]))
    console.log(removeSubSeq([8,6,4,4], [6,4,8]) )
    console.log(removeSubSeq([1,1,2], [1]))
    

提交回复
热议问题