用c++写一个有动态规划算法实现的最长公共子序列问题
时间: 2024-05-20 17:16:04 浏览: 79
#include<stdio.h>
#include<string.h>
int max(int a, int b)
{
return (a > b) ? a : b;
}
void lcs(char *X, char *Y, int m, int n)
{
int L[m+1][n+1];
int i, j;
for(i=0; i<=m; i++)
{
for(j=0; j<=n; j++)
{
if(i == 0 || j == 0)
{
L[i][j] = 0;
}
else if(X[i-1] == Y[j-1])
{
L[i][j] = L[i-1][j-1] + 1;
}
else
{
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
}
int index = L[m][n];
char lcs[index+1];
lcs[index] = '\0';
i = m;
j = n;
while(i > 0 && j > 0)
{
if(X[i-1] == Y[j-1])
{
lcs[index-1] = X[i-1];
i--;
j--;
index--;
}
else if(L[i-1][j] > L[i][j-1])
{
i--;
}
else
{
j--;
}
}
printf("Longest Common Subsequence: %s\n", lcs);
}
int main()
{
char X[] = "AGGTAB";
char Y[] = "GXTXAYB";
int m = strlen(X);
int n = strlen(Y);
lcs(X, Y, m, n);
return 0;
}
//输出: Longest Common Subsequence: GTAB
阅读全文