【HDOJ】1231 最大公共子序列
经典DP。
#include <stdio.h> #define MAXNUM 10005 int array[MAXNUM]; int dp[MAXNUM]; int main() { int n, beg, end, max; int i, k; dp[0] = 0; while (scanf("%d", &n)!=EOF && n) { k = 0; for (i=1; i<=n; ++i) { scanf("%d", &array[i]); if (array[i] >= 0) k = 1; } if (k == 0) { printf("0 %d %d\n", array[1], array[n]); continue; } max = -1; for (i=1; i<=n; ++i) { if (dp[i-1]+array[i] < 0) { dp[i] = 0; } else { dp[i] = dp[i-1]+array[i]; if (dp[i] > max) { max = dp[i]; end = i; } } } k = end-1; while (k) { if (dp[k] == 0) break; k--; } beg = k+1; printf("%d %d %d\n", max, array[beg], array[end]); } return 0; }