繁体   English   中英

当开头和结尾的两个字符相等时的最长回文递归方法为什么我们将计数与字符串的 rest 进行比较?

[英]longest palindrome recursive method when two characters at begin and end are equal why do we compare the count with rest of string?

我正在尝试使用递归方法和动态编程来理解 python 中最长回文的解决方案(A-尚未理解为什么我们称之为动态编程)

我了解基本条件

我知道当两个字符相等时,我们会移动到字符串中的下一个字符,即

c=_longestPalen(st, b+1, e-1, c+2)    

B-但是我们为什么要这样做

return bigger(c, bigger(_longestPalen(st, b+1,e,c), _longestPalen(st, b, e-1, c)))

这是整个代码

def longestPalen(st):
    res=_longestPalen(st, 0,len(st)-1, 0)
    return res

def bigger(x, y):
    if x>y:
        return x
    else:
        return y

def _longestPalen(st, b,e,c):
    if e<b: return c
    if b==e: return c+1
    if st[b] == st[e]:
        c=_longestPalen(st, b+1, e-1, c+2)
        return bigger(c, bigger(_longestPalen(st, b+1,e,c), _longestPalen(st, b, e-1, c)))
    return bigger(_longestPalen(st, b+1, e, 0), _longestPalen(st, b, e-1, 0))

我稍微重组了代码,使其更符合 Python 约定:

def longest_palen(s):
    return _longest_palen(s, 0, len(s) - 1, 0)


def _longest_palen(s, b, e, c):
    if e < b:
        return c
    if b == e:
        return c + 1
    if s[b] == s[e]:
        return max(_longest_palen(s, b + 1, e - 1, c + 2),
                   _longest_palen(s, b + 1, e, c),
                   _longest_palen(s, b, e - 1, c))
    return max(
        _longest_palen(s, b + 1, e, 0),
        _longest_palen(s, b, e - 1, 0)
    )


if __name__ == '__main__':
    print(longest_palen("cyabbaxc"))  # abba   ("cyabbaxc"[2:6])
    print(longest_palen("cabbacxc"))  # cabbac ("cabbacxc"[:6])
    print(longest_palen("cxcabbac"))  # cabbac ("cxcabbac"[2:])


如果当前的第一个和最后一个字符相同,则有四个选项:

  1. 当前的第一个和最后一个字符都在最长回文中。
    • 输入:“阿巴”
    • 最长回文:“abba”(4)
  2. 只有当前的第一个字符在最长回文中。
    • 输入:“cabbacxc”
    • 最长回文:“cabbac”(6)
    • 即使第一个和最后一个字符是“c”,只有第一个在最长回文中,未来的递归步骤需要包括测试“cabbacx”和“cabbac”以获得正确答案。
  3. 只有当前最后一个字符在最长回文中。
    • 输入:“cxcabbac”
    • 最长回文:“cabbac”(6)
    • 即使第一个和最后一个字符是“c”,只有最后一个是最长的回文,未来的递归步骤需要包括测试“xcabbac”和“cabbac”以获得正确答案。
  4. 当前的第一个字符和最后一个字符都不在最长回文中。
    • 输入:“cyabbaxc”
    • 最长回文:“abba”(4)
    • 这种情况将在未来的递归步骤中处理,因为下一步将是“yabbax”,我们将在其中处理“yabba”和“abbax”的测试。

你跑步的原因

c = _longestPalen(str, b + 1, e - 1, c + 2)
return bigger(c, bigger(_longestPalen(str, b + 1, e, c), _longestPalen(str, b, e - 1, c)))

或者

return max(_longest_palen(s, b + 1, e - 1, c + 2),
           _longest_palen(s, b + 1, e, c),
           _longest_palen(s, b, e - 1, c))

在修改后的实现中,就是要考虑所有这些情况。

此行的伪代码类似于:

bigger(both_first_last_included, bigger(only_first_included, only_last_included))

在修改后的实现中,我使用max ,我发现它更清楚地了解正在发生的操作。

max(both_first_last_included, only_first_included, only_last_included)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM