2. 最小編輯距離
Console.Write("輸入原來的英文單字: ");
string b = Console.ReadLine();
Console.Write("輸入改變後的英文單字: ");
string a = Console.ReadLine();
int[,] dp = new int[a.Length + 1, b.Length + 1];
dp[0, 0] = 0;
for (int i = 1; i <= a.Length; i++)
{
dp[i, 0] = i;
}
for (int i = 1; i <= b.Length; i++)
{
dp[0, i] = i;
}
for (int i = 1; i <= a.Length; i++)
{
for (int j = 1; j <= b.Length; j++)
{
dp[i, j] = Math.Min(Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), dp[i - 1, j - 1] + (a[i - 1] == b[j - 1] ? 0 : 2));
}
}
Console.WriteLine($"編輯距離: {dp[a.Length, b.Length]}");Last updated