-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathLongestCommonSubsequence.cs
66 lines (59 loc) · 1.96 KB
/
LongestCommonSubsequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FuzzyString
{
public static partial class ComparisonMetrics
{
public static string LongestCommonSubsequence(this string source, string target)
{
int[,] C = LongestCommonSubsequenceLengthTable(source, target);
return Backtrack(C, source, target, source.Length, target.Length);
}
private static int[,] LongestCommonSubsequenceLengthTable(string source, string target)
{
int[,] C = new int[source.Length + 1, target.Length + 1];
for (int i = 0; i < source.Length + 1; i++) { C[i, 0] = 0; }
for (int j = 0; j < target.Length + 1; j++) { C[0, j] = 0; }
for (int i = 1; i < source.Length + 1; i++)
{
for (int j = 1; j < target.Length + 1; j++)
{
if (source[i - 1].Equals(target[j - 1]))
{
C[i, j] = C[i - 1, j - 1] + 1;
}
else
{
C[i, j] = Math.Max(C[i, j - 1], C[i - 1, j]);
}
}
}
return C;
}
private static string Backtrack(int[,] C, string source, string target, int i, int j)
{
if (i == 0 || j == 0)
{
return "";
}
else if (source[i - 1].Equals(target[j - 1]))
{
return Backtrack(C, source, target, i - 1, j - 1) + source[i - 1];
}
else
{
if (C[i, j - 1] > C[i - 1, j])
{
return Backtrack(C, source, target, i, j - 1);
}
else
{
return Backtrack(C, source, target, i - 1, j);
}
}
}
}
}