【发布时间】:2015-02-19 06:18:20
【问题描述】:
参考问题HERE
我们有两个字符串 A 和 B 具有相同的超级字符集。我们 需要更改这些字符串以获得两个相等的字符串。在每一个动作 我们可以执行以下操作之一:
1- 交换字符串的两个连续字符
2-交换第一个和 字符串的最后一个字符可以对任一字符串执行移动。最小数量是多少 为了获得两个相等的字符串,我们需要多少步?输入 格式和约束:输入的第一行和第二行 包含两个字符串 A 和 B。保证它们的超集 字符相等。 1
看起来这必须使用动态编程来解决。但我无法提出方程式。有人在回答中提出了建议-但看起来不太好。
dp[i][j] =
Min{
dp[i + 1][j - 1] + 1, if str1[i] = str2[j] && str1[j] = str2[i]
dp[i + 2][j] + 1, if str1[i] = str2[i + 1] && str1[i + 1] = str2[i]
dp[i][j - 2] + 1, if str1[j] = str2[j - 1] && str1[j - 1] = str2[j]
}
In short, it's
dp[i][j] = Min(dp[i + 1][j - 1], dp[i + 2][j], dp[i][j - 2]) + 1.
Here dp[i][j] means the number of minimum swaps needs to swap str1[i, j] to str2[i, j]. Here str1[i, j] means the substring of str1 starting from pos i to pos j :)
Here is an example like the one in the quesition,
str1 = "aab",
str2 = "baa"
dp[1][1] = 0 since str1[1] == str2[1];
dp[0][2] = str1[0 + 1][2 - 1] + 1 since str1[0] = str2[2] && str1[2] = str2[0].
【问题讨论】:
-
我认为至少在这个解决方案中不能使用 DP,因为你不能用中间一个元素来改变最后一个元素的位置