reorg and add code for second edition
This commit is contained in:
2
11_dynamic_programming/csharp/01_longest_common_subsequence/.gitignore
vendored
Normal file
2
11_dynamic_programming/csharp/01_longest_common_subsequence/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
**/bin/*
|
||||
**/obj/*
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
|
||||
namespace ConsoleApplication
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var result = LongestCommonSubsequence("fish", "vistafh");
|
||||
Console.WriteLine($"{result.Item1}: {result.Item2}"); // ish: 3
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"buildOptions": {
|
||||
"debugType": "portable",
|
||||
"emitEntryPoint": true
|
||||
},
|
||||
"dependencies": {},
|
||||
"frameworks": {
|
||||
"netcoreapp1.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.1"
|
||||
}
|
||||
},
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
46
11_dynamic_programming/csharp/02_levenshtein/Program.cs
Normal file
46
11_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