/*commom subsequence use an array of two rows ********* ********* just like this each time in turn, because the length of a[i][j] is only defined by a[i-1][j-1] or a[i][j-1] a[i-1][j], so we only need two rows m[i] == n[j] -> a[i][j] = a[i-1][j-1] + 1; m[i] != n[j] -> a[i][j] = max(a[i-1][j], a[i][j-1]); */ #include <stdio.h> #include <string.h> #define MAX_LEN 500 int main() { char m[MAX_LEN], n[MAX_LEN]; int a[2][MAX_LEN+1]; int i = 0, j = 0, turn = 0; int len_n = 0, len_m = 0; memset(m, 0, sizeof(m)); memset(n, 0, sizeof(n)); while (scanf("%s%s", m, n) != EOF) { memset(a, 0, sizeof(a)); len_n = strlen(n); len_m = strlen(m); for (turn = 1, i = 1; i <= len_m; turn = 1-turn, ++i) { for (j = 1; j <= len_n; ++j) { a[turn][j] = m[i-1] == n[j-1] ? (a[1-turn][j-1] + 1) :(a[1-turn][j] > a[turn][j-1] ? a[1-turn][j] : a[turn][j-1]); } } printf("%d/n", a[1-turn][len_n]); } return 0; } 我也写过如此帅气的代码,看到甚是激动啊,看样子要慢慢恢复喽,努力恢复状态 http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1134