using
System;
public
class
GFG {
static
string
s1, s2, s3;
static
string
[][][] dp;
static
string
longestCommonSubsequence;
static
int
[] failureFunction;
public
static
void
Main(
string
[] args)
{
s1 =
"AJKEQSLOBSROFGZ"
;
s2 =
"OVGURWZLWVLUXTH"
;
s3 =
"OZ"
;
Solve();
}
static
string
UpdateString(
string
target,
string
source)
{
if
(target.Length < source.Length) {
return
source;
}
return
target;
}
static
void
Solve()
{
int
lenS1 = s1.Length;
int
lenS2 = s2.Length;
int
lenS3 = s3.Length;
int
j = 0;
failureFunction =
new
int
[111];
Array.Fill(failureFunction, 0);
for
(
int
i = 1; i < lenS3; i++) {
while
(j > 0 && s3[i] != s3[j]) {
j = failureFunction[j - 1];
}
if
(s3[i] == s3[j]) {
failureFunction[i] = ++j;
}
}
dp =
new
string
[111][][];
for
(
int
i = 0; i < 111; i++) {
dp[i] =
new
string
[111][];
for
(
int
k = 0; k < 111; k++) {
dp[i][k] =
new
string
[111];
for
(
int
l = 0; l < 111; l++) {
dp[i][k][l] =
""
;
}
}
}
for
(
int
i = 1; i <= lenS1; i++) {
for
(j = 1; j <= lenS2; j++) {
for
(
int
k = 0; k < lenS3; k++) {
if
(s1[i - 1] == s2[j - 1]) {
char
currentChar = s1[i - 1];
int
temp = k;
while
(temp > 0
&& currentChar != s3[temp]) {
temp
= failureFunction[temp - 1];
}
if
(currentChar == s3[temp]) {
temp++;
}
dp[i][j][temp] = UpdateString(
dp[i][j][temp],
dp[i - 1][j - 1][k]
+ currentChar);
}
dp[i][j][k] = UpdateString(
dp[i][j][k], dp[i - 1][j][k]);
dp[i][j][k] = UpdateString(
dp[i][j][k], dp[i][j - 1][k]);
}
}
}
longestCommonSubsequence =
""
;
for
(
int
i = 0; i < lenS3; i++) {
longestCommonSubsequence
= UpdateString(longestCommonSubsequence,
dp[lenS1][lenS2][i]);
}
if
(longestCommonSubsequence.Length == 0) {
Console.WriteLine(
"-1"
);
}
else
{
Console.WriteLine(longestCommonSubsequence);
}
}
}