【问题标题】:Finding the longest consecutive sequence of integers that appears in both integers查找出现在两个整数中的最长连续整数序列
【发布时间】:2021-06-23 22:06:03
【问题描述】:

给了我两个整数 XY。目标是找到出现在 X 和 Y 中的最长连续整数序列。因此,如果 X = 124534891 和 Y = 324534768,则输出将为 24534因为我们有 124534891 和 324534768。整数可以有不同的长度。

我正在尝试设计一个动态算法解决方案,但我完全迷路了。

【问题讨论】:

  • 您是否尝试过使用大小为 len(X)*len(Y) 的表的方法?
  • integers can be of different length整数的长度是什么? MMXXI 比 2021 年还要长? 整数何时以及如何出现在整数中二十一个中的二十一个?质因数以整数形式出现,例如六分之二和三。

标签: algorithm dynamic dynamic-programming


【解决方案1】:

这是对Longest common substring problem的修改。

将数字视为字符串并应用与 LCS 问题相同的算法。
这是开始使用的伪代码,

function maxConsecutiveSequence(A, B):
    S[N] = toString(A) 
    T[M] = toString(B)
    L = array(N, M)
    len = 0
    ans = {}

    for i = 1 to r
        for j = 1 to n
            if S[i] = T[j]
                if i = 1 or j = 1
                    L[i][j] = 1
                else
                    L[i][j] = L[i - 1][j - 1] + 1
                if L[i][j] > len
                    len = L[i][j]
                    ans = {S[i − z + 1..i]}
                else if L[i][j] = len
                    ans = ans ∪ {S[i − z + 1..i]}
            else
                L[i][j] = 0
    return ans

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多