7 using System.Collections.Generic;
13 public static class LongestCommonSubsequenceExtensions
22 public static Tuple<string, double> LongestCommonSubsequence(
this string input,
string comparedTo,
bool caseSensitive =
false)
24 if (
string.IsNullOrWhiteSpace(input) ||
string.IsNullOrWhiteSpace(comparedTo))
return new Tuple<string, double>(
string.Empty, 0.0d);
27 input = input.ToLower();
28 comparedTo = comparedTo.ToLower();
31 int inputLen = input.Length;
32 int comparedToLen = comparedTo.Length;
34 int[,] lcs =
new int[inputLen + 1, comparedToLen + 1];
35 LcsDirection[,] tracks =
new LcsDirection[inputLen + 1, comparedToLen + 1];
36 int[,] w =
new int[inputLen + 1, comparedToLen + 1];
39 for (i = 0; i <= inputLen; ++i)
42 tracks[i, 0] = LcsDirection.North;
45 for (j = 0; j <= comparedToLen; ++j)
48 tracks[0, j] = LcsDirection.West;
51 for (i = 1; i <= inputLen; ++i)
53 for (j = 1; j <= comparedToLen; ++j)
55 if (input[i - 1].Equals(comparedTo[j - 1]))
57 int k = w[i - 1, j - 1];
59 lcs[i, j] = lcs[i - 1, j - 1] + Square(k + 1) - Square(k);
60 tracks[i, j] = LcsDirection.NorthWest;
65 lcs[i, j] = lcs[i - 1, j - 1];
66 tracks[i, j] = LcsDirection.None;
69 if (lcs[i - 1, j] >= lcs[i, j])
71 lcs[i, j] = lcs[i - 1, j];
72 tracks[i, j] = LcsDirection.North;
76 if (lcs[i, j - 1] >= lcs[i, j])
78 lcs[i, j] = lcs[i, j - 1];
79 tracks[i, j] = LcsDirection.West;
92 while (i > 0 || j > 0)
94 if (tracks[i, j] == LcsDirection.NorthWest)
98 subseq = input[i] + subseq;
102 else if (tracks[i, j] == LcsDirection.North)
107 else if (tracks[i, j] == LcsDirection.West)
113 double coef = p / (inputLen * comparedToLen);
115 Tuple<string, double> retval =
new Tuple<string, double>(subseq, coef);
119 private static int Square(
int p)
125 internal enum LcsDirection