【问题标题】:How to find the Longest Common Subarray of m arrays each of size n?如何找到每个大小为 n 的 m 个数组的最长公共子数组?
【发布时间】:2021-08-29 13:59:06
【问题描述】:

我们有一个由 M 行组成的 2D 矩阵 A,其中每一行都填充了从 1N (A[M][ N])

我们要确定矩阵所有行中最长公共子数组的长度

例子:

A = {{1,2,3,4},{3,4,1,2},{3,1,2,4}}

Longest common Subarray {1,2}

Length of LCS = 2

Output = 2

我不需要代码只是一个优化建议。

【问题讨论】:

  • 阅读tourHow to Ask。 StackOverflow 不是代码编写服务,因此您应该编辑您的诚实尝试,将其作为minimal reproducible example 编码到您的问题中。
  • 您是否尝试过以下解决方案?对你有帮助吗?

标签: c algorithm data-structures array-algorithms


【解决方案1】:

1)遍历每个数组,设第i个数组为A[i]
2)遍历A[i]的每个子数组,计算它们的哈希值并将它们与子数组的
长度一起放入map<pair<hashType, int>, int>计算哈希值出现的次数
3)找出出现n次且长度最大的散列
如果您对以下评论有任何疑问

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多