
题目
对于给定的两个序列X,Y,若Z既是X的子序列也是Y的子序列,则称Z是X和Y的公共子序列。我们需要解决的是求最长公共子序列
代码:
#include<iostream> #include<algorithm> #include<string> using namespace std; static int c[2000][2000] = { 0 }; int main() { string a, b; cin >> a; cin >> b; for (int i = 1; i <= a.length(); i++) for (int j = 1; j <= b.length(); j++) { if (a[i - 1] == b[j - 1]) c[i][j] = c[i - 1][j - 1] + 1; else c[i][j] = max(c[i - 1][j], c[i][j - 1]); } cout << c[a.length()][b.length()]; return 0; }




近期评论