如何将一个字符串转换成与操作的最低数量回文? [英] how to convert a string into a palindrome with minimum number of operations?

查看:225
本文介绍了如何将一个字符串转换成与操作的最低数量回文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是<一href="http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1680"相对=nofollow> 状态问题的字符串转换成与操作的最小数量的回文。我知道这是类似 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.

这篇关于如何将一个字符串转换成与操作的最低数量回文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆