-
StackOverflow 文档
-
algorithm 教程
-
最短的共同超序问题
-
C 语言中最短共同序列问题的实现
public class ShortestCommonSupersequence
{
private static int Max(int a, int b)
{
return a > b ? a : b;
}
private static int Lcs(string x, string y, int m, int n)
{
var l = new int[m + 1, n + 1];
for (var i = 0; i <= m; i++)
{
for (var j = 0; j <= n; j++)
{
if (i == 0 || j == 0) l[i, j] = 0;
else if (x[i - 1] == y[j - 1]) l[i, j] = l[i - 1, j - 1] + 1;
else l[i, j] = Max(l[i - 1, j], l[i, j - 1]);
}
}
return l[m, n];
}
private static int Scs(string x, string y)
{
int m = x.Length, n = y.Length;
int l = Lcs(x, y, m, n);
return m + n - l;
}
public static int Main(string x, string y)
{
return Scs(x, y);
}
}