逐至 2021-04-25 11:17 采纳率: 100%
浏览 26
已结题

c#编程题,最长升序,进链接看文本,会的留下代码最好带下注释

链接: https://pan.baidu.com/s/1gpfD4XVLQ8z95Hho5rRdfw

提取码: kuv8 

  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2021-04-25 13:34
    关注
    using System;
    using System.Linq;
    using System.Collections.Generic;
    namespace ConsoleApp1
    {
        class Program
        {
            public static string findSubsequence(int[] arr)
            {
                // `LIS[i]` stores the longest increasing subsequence of subarray
                // `arr[0…i]` that ends with `arr[i]`
                var LIS = new List<List<int>>();
                for (int i = 0; i < arr.Length; i++)
                {
                    LIS.Add(new List<int>());
                }
    
                // `LIS[0]` denotes the longest increasing subsequence ending at `arr[0]`
                LIS[0].Add(arr[0]);
    
                // start from the second array element
                for (int i = 1; i < arr.Length; i++)
                {
                    // do for each element in subarray `arr[0…i-1]`
                    for (int j = 0; j < i; j++)
                    {
                        // find the longest increasing subsequence that ends with `arr[j]`
                        // where `arr[j]` is less than the current element `arr[i]`
                        if (arr[j] < arr[i] && LIS[j].Count > LIS[i].Count)
                        {
                            LIS[i] = new List<int>();
                            LIS[i].AddRange(LIS[j].ToArray());
                        }
                    }
    
                    // include `arr[i]` in `LIS[i]`
                    LIS[i].Add(arr[i]);
                }
    
    
                return String.Join(" ", LIS.OrderByDescending(i => i.Count).FirstOrDefault());
            }
    
            public static void Main()
            {
                var arr = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();
                Console.WriteLine(findSubsequence(arr));
            }
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化