算法 最长公共子序列(2)
时间:2025-04-20
时间:2025-04-20
算法 最长公共子序列
int m,n;
char *x,*y;
int **b,**c;
void LCSLength(int m,int n,char *y,char *x,int **c,int **b); void LCS(int i,int j,char *x,int**b);
cout<<"请输入两个序列的长度:"<<endl;
cin>>m>>n;
x=new char[m];
y=new char[n];
cout<<"请输入两个序列:"<<endl;
cin>>x>>y;
b=new int *[m + 1];
for (int i=0;i<=m;i++)
b[i]=new int[n + 1];
c=new int *[m + 1];
for (int j=0;j<=m;j++)
c[j]=new int[n + 1];
LCSLength(m,n,x,y,c,b);
cout<<"最长公共子序列的元素个数为"<<c[m][n]<<endl;
cout<<"该序列为:";
LCS(m,n,x,b);
cout<<endl;
return 0;
}
4运行结果
请输入两个序列的长度:
5
5
请输入两个序列:
dfdhh
dfdgh
最长公共子序列的元素个数为4
该序列为:dfdh
Press any key to continue