Dynamic programming - LSC problem

LSC Problem

自底向上

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication17
{
    class Program
    {
        static void Main(string[] args)
        {
            int xLength;
            int yLength;
            Console.WriteLine ("Input the length of X : ");
            xLength = int.Parse(Console.ReadLine());
            Console.WriteLine ("Input the length of Y : ");
            yLength = int.Parse(Console.ReadLine());
            int [,] c = new int [xLength+1,yLength+1];
            int[] x = new int[xLength+1];
            int[] y = new int[yLength+1];
            for (int i = 0; i <= xLength; i++)
            {
                Console.WriteLine("Input each element of X : ");
                x[i] = int.Parse(Console.ReadLine());
                c[i, 0] = 0;
            }
            for (int i = 0; i <= yLength; i++)
            {
                Console.WriteLine("Input each element of Y : ");
                y[i] = int.Parse(Console.ReadLine());
                c[0, i] = 0;
            }
            for (int i = 1; i <= xLength; i++)
            {
                for (int j = 1; j <= yLength; j++)
                {
                    if (x[i] == y[j])
                        c[i, j] = c[i - 1, j - 1] + 1;
                    else if (c[i - 1, j] >= c[i, j - 1])
                        c[i, j] = c[i - 1, j];
                    else c[i, j] = c[i, j - 1];
                }
            }
            int[] pre = new int[0];
            int[] res = generate(pre,c,xLength,yLength,x);
            for (int i = 0; i < res.Length; i++)
            {
                Console.Write("{0} ", res[i]);
            }
            Console.Write("\n");
            for (int i = 0; i < xLength; i++)
            {
                for (int j = 0; j < yLength; j++)
                {
                    Console.Write("{0} ", c[i,j]);
                }
                Console.Write("\n");
            }
            Console.ReadKey();
        }
        static int [] generate(int [] X,int [,] P,int i,int j,int [] Pre) 
        {
            int [] newX = new int [X.Length+1];
            X.CopyTo (newX,0);
            if (i==0 || j == 0)
                return X;
            if (P[i - 1, j] == P[i - 1, j - 1] && P[i - 1, j] == P[i, j - 1] && P[i - 1, j - 1] < P[i, j])
            {
                newX[X.Length] = Pre[i];
                return generate(newX, P, i - 1, j - 1, Pre);
            }
            else if (P[i - 1, j]==P[i, j])
                return generate(X,P,i-1,j,Pre);
            else
                return generate(X, P, i , j-1, Pre);
        }
    }
}

带备忘机制的自顶向下算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            int xLength;
            int yLength;
            Console.WriteLine("Input the length of X : ");
            xLength = int.Parse(Console.ReadLine());
            Console.WriteLine("Input the length of Y : ");
            yLength = int.Parse(Console.ReadLine());
            int[,] c = new int[xLength + 1, yLength + 1];
            int[] x = new int[xLength + 1];
            int[] y = new int[yLength + 1];
            for (int i = 0; i <= xLength; i++)
            {
                Console.WriteLine("Input each element of X : ");
                x[i] = int.Parse(Console.ReadLine());
                c[i, 0] = 0;
            }
            for (int i = 0; i <= yLength; i++)
            {
                Console.WriteLine("Input each element of Y : ");
                y[i] = int.Parse(Console.ReadLine());
                c[0, i] = 0;
            }
            Memo_LSC_Length lsc = new Memo_LSC_Length(c, xLength, yLength, x, y);
            int[] res = lsc.Result();
            for (int i = 0; i < xLength; i++)
            {
                for (int j = 0; j < yLength; j++)
                {
                    Console.Write("{0} ", lsc.Record[i, j]);
                }
                Console.Write("\n");
            }
            for (int i = 0; i < res.Length; i++)
            {
                Console.Write("{0} ", res[i]);
            }
            Console.ReadKey();
        }
    }
    class Memo_LSC_Length
    {
        public Memo_LSC_Length(int[,] x, int xLength, int yLength, int[] m, int[] n)
        {
            Record = x;
            X = xLength;
            Y = yLength;
            M = m;
            N = n;
        }
        public int[,] Record
        {
            get;
            set;
        }
        public int X
        {
            get;
            set;
        }
        public int Y
        {
            get;
            set;
        }
        public int[] M
        {
            get;
            set;
        }
        public int[] N
        {
            get;
            set;
        }
        public void LSC_initial()
        {
            for (int i = 1; i <= X; i++)
            {
                for (int j = 1; j <= Y; j++)
                {
                    Record[i, j] = -1;
                }
            }
        }
        public int LSC_Length(int i, int j)
        {
            if (Record[i, j] >= 0)
                return Record[i, j];
            if (M[i] == N[j])
            {
                Record[i, j] = LSC_Length(i - 1, j - 1) + 1;
                return Record[i, j];
            }
            else
            {
                Record[i, j] = LSC_Length(i, j - 1) > LSC_Length(i - 1, j) ? LSC_Length(i, j - 1) : LSC_Length(i - 1, j);
                return Record[i, j];
            }
        }
        public int[] generate(int[] X, int[,] P, int i, int j, int[] Pre)
        {
            int[] newX = new int[X.Length + 1];
            X.CopyTo(newX, 0);
            //Console.Write("{0} ", newX[X.Length]);
            //Console.Write("Hello,world!!");
            if (i == 0 || j == 0)
            {
                return X;
            }
            if (P[i - 1, j - 1] < P[i, j])
            {
                newX[X.Length] = Pre[i];
                //Console.WriteLine("{0}", newX[X.Length]);
                return generate(newX, P, i - 1, j - 1, Pre);
            }
            else if (P[i - 1, j] == P[i, j])
                return generate(X, P, i - 1, j, Pre);
            else
                return generate(X, P, i, j - 1, Pre);
        }
        public int[] Result()
        {
            LSC_initial();
            LSC_Length(X, Y);
            int[] x = new int[0];
            x = generate(x, Record, X, Y, M);
            return x;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值