本文共 819 字,大约阅读时间需要 2 分钟。
#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;const int MAX = 1000;int c[MAX][MAX];int LCSLength(char *x, char *y){ int m = strlen(x); int n = strlen(y); for(int i=1; i<=m; i++) c[i][0] = 0; for(int j=1; j<=n; j++) c[0][j] = 0; for(int i=1; i<= m; i++) { for(int j=1; j<=n; j++) { if(x[i - 1] == y[j - 1]) c[i][j] = 1 + c[i - 1][j - 1]; else c[i][j] = max(c[i - 1][j], c[i][j - 1]); } } return c[m][n];}int main(int argc, char *argv[]){ char x[MAX], y[MAX]; //freopen("input.txt", "rt", stdin); //freopen("output.txt", "wt", stdout); while(scanf("%s %s", x, y) == 2) { cout << LCSLength(x, y) << endl; } return EXIT_SUCCESS;}
转载地址:http://hdkqb.baihongyu.com/