Upload olympus/wasm_tools/code/lis.c with huggingface_hub
Browse files
olympus/wasm_tools/code/lis.c
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Longest Increasing Subsequence — exact, O(n^2) DP with predecessor tracking.
|
| 2 |
+
*
|
| 3 |
+
* Input: space-separated integers, e.g. "10 9 2 5 3 7 101 18"
|
| 4 |
+
* Output: the LIS as space-separated integers, e.g. "2 3 7 101"
|
| 5 |
+
*
|
| 6 |
+
* This is the algorithm the 3B specialist understands but can't implement:
|
| 7 |
+
* correct DP table + correct predecessor backtracking.
|
| 8 |
+
*/
|
| 9 |
+
|
| 10 |
+
void compute(const char *input) {
|
| 11 |
+
int arr[256];
|
| 12 |
+
int n = 0;
|
| 13 |
+
|
| 14 |
+
/* Parse space-separated integers */
|
| 15 |
+
int pos = 0;
|
| 16 |
+
while (input[pos]) {
|
| 17 |
+
/* Skip spaces */
|
| 18 |
+
while (input[pos] == ' ') pos++;
|
| 19 |
+
if (input[pos] == 0) break;
|
| 20 |
+
|
| 21 |
+
/* Parse number (possibly negative) */
|
| 22 |
+
int neg = 0;
|
| 23 |
+
if (input[pos] == '-') { neg = 1; pos++; }
|
| 24 |
+
int val = 0;
|
| 25 |
+
while (input[pos] >= '0' && input[pos] <= '9') {
|
| 26 |
+
int d = input[pos] - '0';
|
| 27 |
+
int t2 = val + val;
|
| 28 |
+
int t4 = t2 + t2;
|
| 29 |
+
int t8 = t4 + t4;
|
| 30 |
+
val = t8 + t2 + d;
|
| 31 |
+
pos++;
|
| 32 |
+
}
|
| 33 |
+
if (neg) val = 0 - val;
|
| 34 |
+
arr[n] = val;
|
| 35 |
+
n++;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
if (n == 0) { putchar('\n'); return; }
|
| 39 |
+
|
| 40 |
+
/* DP: dp[i] = length of LIS ending at index i */
|
| 41 |
+
int dp[256];
|
| 42 |
+
int prev[256]; /* predecessor index, -1 if none */
|
| 43 |
+
int i, j;
|
| 44 |
+
|
| 45 |
+
for (i = 0; i < n; i++) {
|
| 46 |
+
dp[i] = 1;
|
| 47 |
+
prev[i] = 0 - 1; /* -1 */
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
for (i = 1; i < n; i++) {
|
| 51 |
+
for (j = 0; j < i; j++) {
|
| 52 |
+
if (arr[j] < arr[i] && dp[j] + 1 > dp[i]) {
|
| 53 |
+
dp[i] = dp[j] + 1;
|
| 54 |
+
prev[i] = j;
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
/* Find the index with maximum LIS length */
|
| 60 |
+
int max_len = 0;
|
| 61 |
+
int max_idx = 0;
|
| 62 |
+
for (i = 0; i < n; i++) {
|
| 63 |
+
if (dp[i] > max_len) {
|
| 64 |
+
max_len = dp[i];
|
| 65 |
+
max_idx = i;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
/* Backtrack through predecessors to reconstruct the subsequence */
|
| 70 |
+
int result[256];
|
| 71 |
+
int rlen = 0;
|
| 72 |
+
int idx = max_idx;
|
| 73 |
+
while (idx >= 0) {
|
| 74 |
+
result[rlen] = arr[idx];
|
| 75 |
+
rlen++;
|
| 76 |
+
idx = prev[idx];
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* Print in forward order (result is reversed) */
|
| 80 |
+
for (i = rlen - 1; i >= 0; i--) {
|
| 81 |
+
if (i < rlen - 1) putchar(' ');
|
| 82 |
+
print_int(result[i]);
|
| 83 |
+
}
|
| 84 |
+
putchar('\n');
|
| 85 |
+
}
|