Csharp updates for 4, 9 chapters (#245)
* GetGCDList fix * Csharp longest_common_subsequence update * Csharp levenshtein added * Couple changes in levenshtein
This commit is contained in:
@@ -6,15 +6,67 @@ namespace ConsoleApplication
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var result = LongestCommonSubsequence("fish", "vistafh");
|
||||
Console.WriteLine($"{result.Item1}: {result.Item2}"); // ish: 3
|
||||
}
|
||||
|
||||
if (word_a[i] == word_b[1])
|
||||
public static (string, int) LongestCommonSubsequence(string word1, string word2)
|
||||
{
|
||||
if (string.IsNullOrEmpty(word1) || string.IsNullOrEmpty(word2))
|
||||
return ("", 0);
|
||||
|
||||
string subSeq;
|
||||
var matrix = new int[word1.Length + 1, word2.Length + 1];
|
||||
|
||||
for (int i = 1; i <= word1.Length; i++)
|
||||
{
|
||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||
for (int j = 1; j <= word2.Length; j++)
|
||||
{
|
||||
if (word1[i - 1] == word2[j - 1])
|
||||
{
|
||||
matrix[i, j] = matrix[i - 1, j - 1] + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
matrix[i, j] = Math.Max(matrix[i, j - 1], matrix[i - 1, j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
subSeq = Read(matrix, word1, word2);
|
||||
|
||||
return (subSeq, subSeq.Length);
|
||||
}
|
||||
|
||||
private static string Read(int[,] matrix, string word1, string word2)
|
||||
{
|
||||
string subSeq = null;
|
||||
int x = word1.Length;
|
||||
int y = word2.Length;
|
||||
|
||||
while (x > 0 && y > 0)
|
||||
{
|
||||
cell[i][j] = Math.Max(cell[i - 1][j], cell[i][j - 1]);
|
||||
if (word1[x - 1] == word2[y - 1])
|
||||
{
|
||||
subSeq += word1[x - 1];
|
||||
x--;
|
||||
y--;
|
||||
}
|
||||
else if (matrix[x - 1, y] > matrix[x, y - 1])
|
||||
{
|
||||
x--;
|
||||
}
|
||||
else
|
||||
{
|
||||
y--;
|
||||
}
|
||||
}
|
||||
|
||||
var charArray = subSeq.ToCharArray();
|
||||
Array.Reverse(charArray);
|
||||
subSeq = new string(charArray);
|
||||
|
||||
return subSeq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
09_dynamic_programming/csharp/02_levenshtein/Program.cs
Normal file
46
09_dynamic_programming/csharp/02_levenshtein/Program.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
|
||||
namespace ConsoleApplication
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static int LevenshteinDistance(string source, string target)
|
||||
{
|
||||
var matrix = CreateMatrix(source, target);
|
||||
|
||||
for (int i = 1; i <= source.Length; i++)
|
||||
{
|
||||
for (int j = 1; j <= target.Length; j++)
|
||||
{
|
||||
matrix[i, j] = Math.Min(matrix[i, j - 1] + 1, Math.Min(
|
||||
matrix[i - 1, j] + 1,
|
||||
matrix[i - 1, j - 1] + (source[i - 1] != target[j - 1] ? 1 : 0)));
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[source.Length, target.Length];
|
||||
}
|
||||
|
||||
private static int[,] CreateMatrix(string source, string target)
|
||||
{
|
||||
var matrix = new int[source.Length + 1, target.Length + 1];
|
||||
|
||||
if (source.Length < target.Length)
|
||||
{
|
||||
(source, target) = (target, source);
|
||||
}
|
||||
|
||||
for (int i = 0; i <= source.Length; i++)
|
||||
{
|
||||
matrix[i, 0] = i;
|
||||
|
||||
if (i <= target.Length)
|
||||
{
|
||||
matrix[0, i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user