Created
October 4, 2011 03:54
-
-
Save limitedmage/1260867 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int seq[4040]; | |
int dyn[4040]; | |
int n; | |
int lis() { | |
// longest increasing subsequence | |
// for sequence seq of length n | |
// O(nlgn) | |
// for longest decreasing subsequence, multiply elements by -1 | |
// for longest common subsequence with unique elements, map | |
// to order of first string and do lis | |
int size = 1; | |
dyn[0] = seq[0]; | |
for (int i = 1; i < n; i++) { | |
int curr = seq[i]; | |
int last = dyn[size - 1]; | |
if (last < curr) { | |
dyn[size] = curr; | |
size++; | |
} else if (last > curr) { | |
int *place = lower_bound(dyn, dyn + size, curr); // binsearch from <algorithm> | |
if (*place > curr) { | |
*place = curr; | |
} | |
} | |
} | |
return size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment