如何将一个字符串转换成与操作的最低数量回文?回文、转换成、字符串、如何将

2023-09-11 03:09:50 作者:Redundant 多余

下面是 状态问题的字符串转换成与操作的最小数量的回文。我知道这是类似 Levenshtein距离  但我解决不了这些事

Here is the problem states to convert a string into a palindrome with minimum number of operations. I know it is similar to the Levenshtein distance but I can't solve it yet

例如,输入mohammadsajjadhossain输出为8

推荐答案

执行上的字符串和反向Levenshtein距离。该解决方案将是最小的对角线在DP阵列从去的操作的左下角到右上角,以及每个条目的正上方和正下方的对角线

Perform Levenshtein distance on the string and its reverse. The solution will be the minimum of the operations in the diagonal of the DP array going from bottom-left to top-right, as well as each entry just above and just below the diagonal.

这工作,因为沿着对角线重新present条目,使首我所需要的最小的编辑和字符串等于最后倪人物和作品正上方和下方再present最小的字符串结束与奇数长度,其中中间的(遗留)字符不针对任何匹配。

This works because the entries along the diagonal represent the minimum edits required to make the first i and last N-i characters of the string equal and the entries just above and just below represent the minimum for strings ending up with odd-length where the middle (left-over) character doesn't match against anything.