Dynamic programming - 最长单调递增子串问题

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;
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值