最长公共子序列
// zuidazixulie.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include<string> using namespace std; char b[10][10]; int c[10][10]; void Lcs(string x, string y) { int m = x.length(); int n = y.length(); for (int i = 1;i <= m; i++) c[i][0] = 0; for (int j = 0; j <= n; j++) c[0][j] = 0; for(int i=1;i<=m;i++) for (int j = 1; j <= n; j++) { if (x[i] == y[j]) { c[i][j] = c[i - 1][j - 1] + 1; b[i][j] = 'x'; } else if (c[i - 1][j] >= c[i][j - 1]) { c[i][j] = c[i - 1][j]; b[i][j] = '|'; } else { c[i][j] = c[i][j-1]; b[i][j] = '-'; } } } void print(string x, int i, int j) { if (i == 0 || j == 0) return; if (b[i][j] == 'x') { print(x, i - 1, j - 1); cout << x[i]; } else if (b[i][j] = '|') print(x, i - 1, j); else print(x, i,j - 1); } int main() { string x = "ABCD"; string y = "bBCD"; Lcs(x, y); print(x, 4, 3); while (1); return 0; }