int main() { char X[] = "ABCBDAB"; char Y[] = "BDCABA"; int lcs = LCS(X, Y); cout << "LCS length: " << lcs << endl; return 0; }字符串改为由我输入
时间: 2024-01-07 19:02:53 浏览: 82
lcs1_LCS_cpp_
好的,你可以试着修改以下代码:
```
#include <iostream>
#include <cstring>
using namespace std;
int LCS(char X[], char Y[]) {
int m = strlen(X);
int n = strlen(Y);
int c[m+1][n+1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
c[i][j] = 0;
} else if (X[i-1] == Y[j-1]) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = max(c[i-1][j], c[i][j-1]);
}
}
}
return c[m][n];
}
int main() {
char X[100], Y[100];
cout << "Enter the first string: ";
cin >> X;
cout << "Enter the second string: ";
cin >> Y;
int lcs = LCS(X, Y);
cout << "LCS length: " << lcs << endl;
return 0;
}
```
这样你就可以在运行程序时,输入两个字符串来计算它们的最长公共子序列长度了。
阅读全文