最长公共子串

#include<iostream>
using namespace std;

//动态规划解决最长公共子串
void LCSLength(int m,int n,char *x,char *y,int **c,int **b)

{
int i,j;
for(i=1;i<=m;i++) c[i][0]=0;
for(i=1;i<=n;i++) c[0][i]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(x[i]==x[j])
{
c[i][j]=c[i-1][j-1]+1;b[i][j]=1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];b[i][j]=2;
}
else
{
c[i][j]=c[i][j-1];b[i][j]=3;
}
}
}

//输出最长公共子序列
void LCS(int i,int j,char *x,int **b)

{
if(i==0||j==0) return;
if(b[i][j]=1)
{
LCS(i-1,j-1,x,b);cout<<x[i];
}
else if(b[i][j]==2)
LCS(i-1,j,x,b);
else LCS(i,j-1,x,b);
}
Live together,or Die alone!
原文地址:https://www.cnblogs.com/hzhida/p/2354723.html