两个数组中最长的数字序列

时间:2017-11-13 10:31:19

标签: javascript compare

我有两个数组,在0-9之间以随机数初始化 我需要找到两者中存在的最长的相同序列。

例如

var arrFirst = [8,5,3,0,3,1,8,2,8,7,3,5,4,0,8,5,7,3,1]
var arrSecond = [8,5,4,0,2,9,4,3,6,9,2,3,1,8,2,8,7,3,3]

结果数组需要包含两个数组

中出现的最长序列
var arrResult = [3,1,8,2,8,7,3]

在这个例子中,我们在开始[8,5]时有序列,但它不是最长的序列。

请您提供一些可以帮助我的代码吗?

感谢名单

4 个答案:

答案 0 :(得分:1)

伪代码:

1)create two more arrays for longest sequence and another one for running sequence
2)Start looking for matches between 2 original arrays
3)Keep current sequence in runningSequence array
4)check if arrays match
  5)if so check if length of running sequence is longer than theLongestSequence array
    6)If so, replace the longestSequenceArray content with runningSequence array and keep going
    7)if not, proceed to next char position
8)Repeat until all chars in original arrays are processed by going back to step 4
9)Print Longest array content

您有责任为上述逻辑编写代码。

祝你好运。

答案 1 :(得分:0)

使用这样来搜索第二个数组中存在的最大长度



var arrFirst = [8,5,3,0,3,1,8,2,8,7,3,5,4,0,8,5,7,3,1];
var arrSecond = [8,5,4,0,2,9,4,3,6,9,2,3,1,8,2,8,7,3,3];
var firstStr = arrFirst.join("");
var secondStr = arrSecond.join("");
var combinations = getCombination(firstStr);
combinations = combinations.sort(function(a, b){
  return b.length - a.length;
});
for(var i in combinations){
  if(secondStr.indexOf(combinations[i]) !== -1){
    console.log(combinations[i]+" found with length : "+combinations[i].length);
    console.log(combinations[i].split(""));
    break;
  }
}

function getCombination(str) {
    var fn = function(active, rest, a) {
        if (!active && !rest)
            return;
        if (!rest) {
            if(firstStr.search(active) !== -1){
              a.push(active);
            }
        } else {
            fn(active + rest[0], rest.slice(1), a);
            fn(active, rest.slice(1), a);
        }
        return a;
    }
    return fn("", str, []);
}




答案 2 :(得分:0)

您可以使用array1的每个元素检查array2的ech元素,然后使用带有计数器的while循环检查元素,并为每个数组检索索引的偏移量

稍后检查找到的相同元素的长度是否大于或等于最后找到的数组,并将结果集替换为新的更长的一个或附加,如果等于结果集。

var array1 = [8, 5, 3, 0, 3, 1, 8, 2, 8, 7, 3, 5, 4, 0, 8, 5, 7, 3, 1],
    array2 = [1, 8, 4, 0, 2, 9, 4, 3, 6, 9, 2, 3, 1, 8, 2, 8, 7, 3, 3],
    result = array1.reduce(function (r, _, i, a) {
        array2.forEach(function (__, j, b) {
            var k = 0,
                temp = [];

            while ((i + k) in a && (j + k) in b && a[i + k] === b[j + k]) {
                temp.push(a[i + k]);
                k++;
            }
            if (!r.length || temp.length > r[0].length) {
                r = [temp];
                return;
            }
            if (temp.length === r[0].length) {
                r.push(temp);
            }
        });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

我正在发布用C ++编写的此任务的解决方案,以防万一有人需要它。

void clear(int *ar, int n)
{
    for (int i = 0; i < n; i++)
    {
        ar[i] = 0;
    }
}

void copy(int*first, int *second, int n)
{
    for (int i = 0; i < n; i++)
    {
        first[i] = second[i];
    }
}

int main() {

    int ar_1[] = { 8,5,3,0,3,1,8,2,8,7,3,5,4,0,8,5,7,3,1 };
    int ar_2[] = { 8,5,4,0,2,9,4,3,6,9,2,3,1,8,2,8,7,3,3 };

    const int arr_size = sizeof(ar_1) / sizeof(int);

    int arr_longest[arr_size] = { 0 };
    int sequence_counter = 0;

    int arr_temp[arr_size] = { 0 };
    int temp_counter = 0;

    for (int i = 0; i < arr_size; i++)
    {
        bool match = false;
        int inner_counter = 0;

        for (int k = 0; k < arr_size; k++)
        {
            if (match)
            {
                if (ar_1[++inner_counter] == ar_2[k])
                {
                    arr_temp[temp_counter++] = ar_2[k];
                }
                else
                {
                    if (temp_counter > sequence_counter)
                    {
                        sequence_counter = temp_counter;
                        copy(arr_longest, arr_temp, arr_size);
                    }

                    clear(arr_temp, arr_size);
                    temp_counter = 0;
                    match = false;
                }
            }
            else if (ar_1[i] == ar_2[k])
            {
                arr_temp[temp_counter++] = ar_1[i];
                inner_counter = i;
                match = true;
            }
        }
    }
}