From 2ad9f2578548073b5658f6bb07cded6546f76671 Mon Sep 17 00:00:00 2001 From: KiritoTRw <3021577574@qq.com> Date: Thu, 11 Aug 2016 09:05:46 +0800 Subject: [PATCH] Create LIS.cpp --- .ACM-Templates/LIS.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .ACM-Templates/LIS.cpp diff --git a/.ACM-Templates/LIS.cpp b/.ACM-Templates/LIS.cpp new file mode 100644 index 0000000..9ac1f9b --- /dev/null +++ b/.ACM-Templates/LIS.cpp @@ -0,0 +1,30 @@ +#include +#define MAXN 1010 +int seq[MAXN]; +int seqlen[MAXN]; +int main() +{ + int N; + while(scanf("%d",&N)==1) + { + int i,j,k,max,maxlen=1; + for(i=1; i<=N; i++) + seqlen[i]=1; //seqlen数组存以第i个数为终点的最长上升序列 + for(i=1; i<=N; i++) + scanf("%d",&seq[i]); //seq数组保存序列数组 + for (i=2; i<=N; i++) + { + max=0; + for (j=1; j<=i-1; j++) + { + if(seq[j]max) //在前i-1个序列中,寻找以终点小于seq[i]的最长的子序列,即最优子状态 + max=seqlen[j]; + } + seqlen[i]=max+1; + if(seqlen[i]>maxlen) //seqlen中保存的是第i个数为终点的最长上升序列,找出这个数组中最大的值即为最优序列长度 + maxlen=seqlen[i]; + } + printf("%d\n",maxlen); + } + return 0; +}