2

这是我的问题:我在javascript中有5个这样的整数数组:

array1 = [0, 1, 2, 3, 4];
array2 = [9, 1, 2, 3, 4];
array3 = [10, 1, 2, 11, 4];
array4 = [12, 1, 2, 13, 4];
array5 = [14, 1, 2, 15, 4];

我必须找到最长的公共子数组。在这种情况下,我必须检索以下子数组:[1, 2, 4].

对于记录,我不会在数组中找到重复,我的主要目标不是执行速度。

谢谢

4

5 回答 5

2

这是在 Javascript 中使用 Set 的解决方案

var myArray = [array1 , array2 ,array3 , array4 ,array5];
let keys = new Set();
myArray.forEach(arr => arr.forEach(el => keys.add(el) ))
var common = [...keys].filter(key => myArray.every(arr => arr.includes(key)))
console.log(common);
于 2021-06-20T14:17:28.850 回答
1
#define MAX(a,b) a>b?a:b

int main(int argc, char* argv[])
{
        if(argc < 2)
                return -1;

        int x = strlen(argv[1])+1;
        int y = strlen(argv[2])+1;

        int i,j,k,l;

        int longest =0;
        char* LCS = (char*)malloc(sizeof(char)*MAX(x,y));

        int** arr  = (int**)malloc(sizeof(int*)*x);

        for(i=0;i<=y;i++)
        arr[i] =(int*) malloc(sizeof(int)*y);

        for(i=0;i<=x;i++)
            for(j=0;j<=y;j++)
            {
                arr[i][j] = 0;
            }

        for(i=0;i<x;i++)
                for(j=0;j<y;j++)
                {
                        if(argv[1][i] == argv[2][j])
                                arr[i+1][j+1] = arr[i][j]+1;
                        if(arr[i+1][j+1] > longest)
                        {
                                longest =arr[i+1][j+1];
                                memset(LCS,0,MAX(x,y));
                                for( k=0,l=i;k<=longest;k++,l--)
                                        LCS[k] = argv[1][l];

                        }
                }

        printf(" %s",argv[2]);
        for(i=0;i<x;i++)
        {
              printf("\n%c",argv[1][i]);              
              for(j=0;j<y;j++)
              {
                printf("%d",arr[i][j]);                                                                                   
              }
        }

        printf("\nLongest Common Subarray : %s\n",LCS);
        return 0;
}
于 2013-08-02T09:32:37.910 回答
0

尝试这个:

var array1 = [0, 1, 2, 3, 4];
var array2 = [9, 1, 2, 3, 4];
var array3 = [10, 1, 2, 11, 4];
var array4 = [12, 1, 2, 13, 4];
var array5 = [14, 1, 2, 15, 4];

// join everything into one array
var all = array1.join(',')+','+array2.join(',')+','+array3.join(',')+','+array4.join(',')+','+array5.join(',');
all = all.split(',');

// get an object with all unique numbers as keys
var keys = {};
for(var i=0; i<all.length; i++) keys[all[i]] = 1;
console.log(keys);

// generate an array with values present in all arrays
var common = [];
for(var x in keys) {
  if(array1.indexOf(parseInt(x)) != -1 && array2.indexOf(parseInt(x)) != -1 && array3.indexOf(parseInt(x)) != -1 && array4.indexOf(parseInt(x)) != -1 && array5.indexOf(parseInt(x)) != -1) {
    common.push(x);
  }
}
console.log(common);
于 2012-11-29T15:10:49.857 回答
0

我想这可以给你一个好的开始:我的脚本会返回一个包含每个元素计数的对象。但是现在,它以第一个数组为基础。

var array1 = [0, 1, 2, 3, 4];
var array2 = [9, 1, 2, 3, 4];
var array3 = [10, 1, 2, 11, 4];
var array4 = [12, 1, 2, 13, 4];
var array5 = [14, 1, 2, 15, 4];
var array6 = [13, 1, 2, 18, 4];

var mainArr = [array1, array2, array3, array4, array5, array6]

function getCommonElement(arr){
    var subLength = arr[0].length;
    var resultArr = new Array();
    var ret = new Object();
    for(var k=0;k<subLength;k++){
        var temp = new Array();
        for(var i=0;i<arr.length;i++){
            temp.push(arr[i][k]);
        }
        resultArr.push(temp);
    }
    for(var i=0;i<arr[0].length;i++){
        ret[arr[0][i]+''] =   resultArr[i].join('').split(arr[0][i]+'').length - 1;
    }
    return ret;
}

干杯。

于 2012-11-29T15:34:18.937 回答
0

/** 最长公共子数组 b/w 2 个数组

a = [2,3,4,5,6,7,8],b = [6,7,8,4,5,2,3]

答案 = 6,7,8

基本上创建一个 2d arr 并且如果元素匹配 dp[i][j] = 1 + dp[i-1][j-1];

如果 dp[i][j] > maxLen,更新 maxLen 并存储索引现在我们有了 maxLen,子数组将从 (index - maxLen) 到索引。

*/

int[] finMaxCommon(int[] a, int[] b){

    int m = a.length, n = b.length, maxLen = 0;
    int[][] dp = new int[m+1][n+1];
    
    // i want a 0th row why? m->out of bounds; comparing i-1; i->1 then i-1 will be 0 
    
    for (int i = 1; i<=m; i++){
        for(int j = 1; j<=n; j++){
            if(a[i-1] == b[j-1]) {
                dp[i][j] = 1 + dp[i-1][j-1];
                maxLen = Math.max(maxLen, dp[i][j]);
            }
        }
    }
    // endIndex = 6, 3, a[6-3+1], a[6]
    return new int[]{a[endIndex-maxLen+1], [endIndex]}; 

}


dry run
  0,6,7,8,4,5,2,3
0,          0     //
2,            1   // (2,2) i = 1, j = 6 1 + dp[0][5]
3,              2 // (3,3) i = 2, j = 7 1 + dp[1][6]
4,
5,
6,  1
7,    2
8,      3
于 2021-03-09T06:22:32.183 回答