求两个字符串的最长公共子序列
#include<cstdio>
#include<cstring>
int maximum_length_common_subsequence(char *op,char *jk);
int max(int a,int b);
int main()
{
char op[505],jk[505];
while(scanf("%s%s",op,jk)!=EOF)
{
int a=maximum_length_common_subsequence(op,jk);
printf("%d\n",a);
}
return 0;
}
int maximum_length_common_subsequence(char *op,char *jk)
{
int i=strlen(op),j=strlen(jk),x,y;
int nm[505][505];
memset(nm,0,sizeof(nm));
for(x=1;x<=i;x++)
for(y=1;y<=j;y++)
{
if(op[x-1]==jk[y-1])
nm[x][y]=nm[x-1][y-1]+1;
else
nm[x][y]=max(nm[x-1][y],nm[x][y-1]);
}
return nm[i][j];
}
int max(int a,int b)
{
return a>b?a:b;
}
本文介绍了一种计算两个字符串最长公共子序列的算法实现。通过动态规划方法,该程序能够找出给定两个字符串的最长公共子序列长度。文章提供了一个完整的C语言程序示例,并详细解释了核心函数的工作原理。
249

被折叠的 条评论
为什么被折叠?



