public string FindLcs(string x, string y) { var context = new LcsSearchContext(); context.X = x; context.Y = y; context.Output = new StringBuilder(); context.LcsLengthsTable = BuildLcsLengthsTable(x, y); FindLcs(context, x.Length, y.Length); return(context.Output.ToString()); }
private void FindLcs(LcsSearchContext context, int lengthOfX, int lengthOfY) { if (lengthOfX == 0 || lengthOfY == 0) { return; } if (context.LcsLengthsTable[lengthOfX, lengthOfY].Direction == SearchDirection.Diagonal) { FindLcs(context, lengthOfX - 1, lengthOfY - 1); context.Output.Append(context.X[lengthOfX - 1]); } else if (context.LcsLengthsTable[lengthOfX, lengthOfY].Direction == SearchDirection.Up) { FindLcs(context, lengthOfX - 1, lengthOfY); } else { FindLcs(context, lengthOfX, lengthOfY - 1); } }