Longest Increasing Subsequence - C plus plus Code, Projects for Computer Programming
vishv
vishv8 October 2013

Longest Increasing Subsequence - C plus plus Code, Projects for Computer Programming

PDF (46 KB)
1 page
3Number of download
1000+Number of visits
Description
Finding a longest increasing sub-sequence in given method sounds tough and difficult task. Here is running C++ code for LIS to help you out. Related key words are: Longest Increasing Subsequence, LIS, Longest Increasing ...
20 points
Download points needed to download
this document
Download the document
Preview1 page / 1
Download the document
Microsoft Word - LIS

#include<stdio.h> #define MAXN 100 int A[MAXN], N; int L[MAXN], P[MAXN]; void printPath(int x) { if(P[x] == -1) { printf("%d",x); return; } printPath(P[x]); printf(" %d",x); } int doLIS() { int i, j; int longest = 0, last = 0; for(i = 1; i<=N; i++) { int max = 0; int parent = -1; for(j = i-1; j>=0; j--) { if(A[i] > A[j]) { if(L[j] > max) { max = L[j]; parent = j; } } } L[i] = max+1; P[i] = parent; if(L[i] > longest) { longest = L[i]; last = i; } } printf("LIS : "); printPath(last); puts(""); printf("Length: %d\n",longest); return longest; } int main() { int i; while(scanf("%d",&N) == 1) { A[0] = 0; for(i = 1; i<= N; i++) { scanf("%d",&A[i]); } doLIS(); } return 0; } 

docsity.com

comments (0)

no comments were posted

be the one to write the first!

Download the document