Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
public class ShortestCommonSupersequence
|
||||
{
|
||||
Dictionary<(string, string), string> cache = new();
|
||||
|
||||
public string scs(string x, string y)
|
||||
{
|
||||
if (x.Length == 0) return y;
|
||||
if (y.Length == 0) return x;
|
||||
|
||||
if (cache.TryGetValue((x, y), out var result)) return result;
|
||||
|
||||
if (x[0] == y[0])
|
||||
{
|
||||
return cache[(x, y)] = x[0] + scs(x.Substring(1), y.Substring(1));
|
||||
}
|
||||
|
||||
var xr = scs(x.Substring(1), y);
|
||||
var yr = scs(x, y.Substring(1));
|
||||
if (yr.Length <= xr.Length)
|
||||
{
|
||||
return cache[(x, y)] = y[0] + yr;
|
||||
}
|
||||
else
|
||||
{
|
||||
return cache[(x, y)] = x[0] + xr;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var scs = new ShortestCommonSupersequence();
|
||||
Console.WriteLine(scs.scs("abcbdab", "bdcaba"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue