zoukankan      html  css  js  c++  java
  • 求数组中最长递增子序列

    #include<iostream>
    using namespace std;
    
    //method1
    /*
    利用动态规划来求解。
    假设目标数组array[]的前i个元素中,最长递增子序列的长度为LIS[i].那么,
    LIS[i]=max{1,LIS[k]+1},array[i+1]>array[k],for any k<=i
    时间复杂度为O(N^2)
    */
    int findLongest(int* A,int N)
    {
        int* LIS=new int[N];
        int max=0;
        for(int i=0;i<N;i++)LIS[i]=1;
           for(int i=0;i<N;i++)
        for(int j=0;j<i;j++)
        {
         if(A[i]>A[j] && LIS[j]+1>LIS[i])
          {
               LIS[i]=LIS[j]+1;
          }
        }
       
         for(int i=0;i<N;i++)
      {
        if(LIS[i]>max)max=LIS[i];
      }
    delete [] LIS; return max; } int main() { int N; int* A; while(cin>>N,N) { A=new int[N]; for(int i=0;i<N;i++)cin>>A[i]; int max=findLongest(A,N); cout<<"数组中最长递增子序列长度为:"<<max<<endl; }
    delete [] A; system("pause"); return 0; }

      

  • 相关阅读:
    js返回到顶部
    css培训一
    css常用hack技巧
    css培训二
    css样式渲染规则
    html语义(一)
    css样式表管理
    html+css培训方案
    继承
    封装
  • 原文地址:https://www.cnblogs.com/yanglf/p/2758582.html
Copyright © 2011-2022 走看看