公共递增子序列作为公共子序列的变型算法,在一个无序的数组中,找出这个数组的最大公共子序列。C#代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 递增子序列
{
class Program
{
static int[] b = new int[8]; //全局数组b用来保存最大子序列长度
static int[] a = new int[8];
public static int LISDyna()
{
int i, j, k;
for(i=1,b[0]=1;i<8;i++)
{ for (j = 0, k = 0; j < i; j++)
{
if (a[j] <= a[i] && k < b[j])
{ k = b[j]; }
b[i] = k + 1;
}
}
return maxL(8);
}
static int maxL(int n)
{
int tmp = 0;
for (int i = 0; i < n; i++)
{
if (b[i] > tmp)
{ tmp=b[i]; }
}
return tmp;
}
static void Main(string[] args)
{
for (int i = 0; i < a.Length; i++)
{ a[i] = Convert.ToInt32(Console.ReadLine()); }
int c = LISDyna();
Console.WriteLine(LISDyna());
for (int j = 0; j < b.Length; j++)
{ Console.Write(Convert.ToInt32(b[j])); }
Console.ReadKey();
}