forked from c910335/LCS-Problem-in-Linear-Space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
66 lines (58 loc) · 2 KB
/
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
namespace csharp
{
public static class StringHelper
{
public static string Reverse(this string input)
{
return new string(input.ToCharArray().Reverse().ToArray());
}
}
class Program
{
static int[] GetScores(string x, string y)
{
int[] scores = new int[y.Length + 1];
int preScore = 0;
foreach(var xc in x)
{
preScore = 0;
for(int i = 0; i < y.Length; ++i)
{
var yc = y[i];
var tmp = scores[i + 1];
scores[i + 1] = new int[] { xc == yc ? preScore + 1 : 0, scores[i], scores[i + 1] }.Max();
preScore = tmp;
}
}
return scores;
}
static string Filter(string text, string pattern)
{
return text.Contains(pattern) ? pattern : string.Empty;
}
static string GetLCS(string x, string y)
{
if (x.Length == 1) return Filter(y, x);
if (y.Length == 1) return Filter(x, y);
int xmid = x.Length / 2;
var topScores = GetScores(x.Substring(0, xmid), y);
var bottomScores = GetScores(x.Substring(xmid).Reverse(), y.Reverse()).Reverse().ToArray();
var scoresSum = topScores.Zip(bottomScores, (a, b) => a + b).ToArray();
var maxScore = scoresSum.Max();
int ymid = Array.IndexOf(scoresSum, maxScore);
return maxScore == 0 ? string.Empty : GetLCS(x.Substring(0, xmid), y.Substring(0, ymid)) + GetLCS(x.Substring(xmid), y.Substring(ymid));
}
static void Main(string[] args)
{
int t = int.Parse(Console.ReadLine());
while (t-- > 0)
{
var input = Console.ReadLine().Split(' ');
Console.WriteLine(GetLCS(input[0], input[1]));
}
}
}
}