python - python中最长重复子序列长度使用递归产生错误结果
问题描述
我正在尝试生成最长的重复子序列,但在某些情况下输出似乎不正确。
下面是我正在使用的代码块
def LRSLength(X, m, n):
# return if the end of either string is reached
if m == 0 or n == 0:
return 0
# if characters at index `m` and `n` matches and the index are different
if X[m - 1] == X[n - 1] and m!=n:
return LRSLength1(X, m - 1, n - 1) + 1
# otherwise, if characters at index `m` and `n` don't match
return max(LRSLength(X, m, n - 1), LRSLength(X, m - 1, n))
虽然,当我打电话时:
LRSLength([1,1],2,2),输出=1。
LRSLength1([1,1,1,1],4,4),输出=3
LRSLength1([1,1,1,1,2,2,1,1,1,1,1],11,11),输出=8
谁能帮助我哪里出错了?
解决方案
推荐阅读
- python-3.x - 使用散景滑块过滤绘图上的数据
- php - 我如何使用 laravel 获得今天、过去 7 天和过去 30 天的利润
- jquery - 使用 JQuery 编写新的 HTML 会禁用弹出框
- java - 我如何知道从哪个类异常扩展?
- blockchain - Solidity 函数在 remix 中返回 true,但在 web3 函数调用中返回 false
- powershell - 通过 Windows 任务调度器打开缩放并开始会议
- python - 用漂亮的汤替换和更新输入标签
- python - 如果在python中我们得到valueERRor怎么办
- c++ - 如何将十六进制数据放入 QT 列表小部件
- wordpress - WordPress 和谷歌云平台的问题