using
System;
using
System.Collections.Generic;
class
GFG
{
static
int
N = 100;
static
int
[,]L =
new
int
[N, N];
static
HashSet<String> findLCS(String X,
String Y,
int
m,
int
n)
{
HashSet<String> s =
new
HashSet<String>();
if
(m == 0 || n == 0)
{
s.Add(
""
);
return
s;
}
if
(X[m - 1] == Y[n - 1])
{
HashSet<String> tmp = findLCS(X, Y, m - 1, n - 1);
foreach
(String str
in
tmp)
s.Add(str + X[m - 1]);
}
else
{
if
(L[m - 1, n] >= L[m, n - 1])
s = findLCS(X, Y, m - 1, n);
if
(L[m, n - 1] >= L[m - 1, n])
{
HashSet<String> tmp = findLCS(X, Y, m, n - 1);
foreach
(String str
in
tmp)
s.Add(str);
}
}
return
s;
}
static
int
LCS(String X, String Y,
int
m,
int
n)
{
for
(
int
i = 0; i <= m; i++)
{
for
(
int
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] = Math.Max(L[i - 1, j],
L[i, j - 1]);
}
}
return
L[m, n];
}
public
static
void
Main(String[] args)
{
String X =
"AGTGATG"
;
String Y =
"GTTAG"
;
int
m = X.Length;
int
n = Y.Length;
Console.WriteLine(
"LCS length is "
+
LCS(X, Y, m, n));
HashSet<String> s = findLCS(X, Y, m, n);
foreach
(String str
in
s)
Console.WriteLine(str);
}
}