using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LPS
{
class Program
{
static void Main(string[] args)
{
int[] pre = { 9, 2, 3, 4, 5, 6 };
int[,] rec = new int[pre.Length, pre.Length];
Lps lps = new Lps(rec, pre.Length, pre);
lps.Lps_initial();
lps.Lps_length();
rec = lps.Record;
int [] res = res = lps.generate ();
for (int i = 0; i < pre.Length; i++)
{
for (int j = 0; j < pre.Length; j++)
{
Console.Write("{0} ", rec[i, j]);
}
Console.Write("\n");
}
for (int i = res[0]; i <= res[1];i++ )
Console.Write("{0} ",pre[i]);
Console.ReadKey();
}
}
class Lps
{
public Lps(int [,] record,int x,int [] origin)
{
Record = record;
Length = x;
OriginSeq = origin;
}
public int[,] Record
{
get;
set;
}
public int Length
{ set; get; }
public int[] OriginSeq
{ get; set; }
public void Lps_initial()
{
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Length; j++)
{
if (i == j)
Record[i, j] = 1;
else
Record[i, j] = 0;
}
}
}
public void Lps_length()
{
for (int l = 2; l <= Length; l++)
{
for (int i = 0; i < Length - l + 1; i++)
{
int j = i + l - 1;
if (Record[i, j - 1] == 0)
Record[i, j] = 0;
else if (OriginSeq[j - 1] < OriginSeq[j])
Record[i, j] = Record[i, j - 1] + 1;
else
Record[i, j] = 0;
}
}
}
public int[] generate()
{
int temp=0;
int [] rec = {0,0};
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Length; j++)
{
if (Record[i, j] > temp)
{
temp = Record[i, j];
rec[0] = i;
rec[1] = j;
}
}
}
return rec;
}
}
}
Dynamic programming - 最长单调递增子串问题
最新推荐文章于 2019-05-27 19:41:31 发布