#include"AllLcs.h"
#include <iostream>
using namespace std;
//初始化数组X,Y
AllLcs::AllLcs(char* x,char* y)
{
X=x;
Y=y;
m=strlen(X);
n=strlen(Y);
}
//求出数组C,b的值
void AllLcs::LCS_L()
{
//int m=strlen(X);
//int n=strlen(Y); //求出数组X,Y的长度
int **C = new int*[m+1];//数组C 数组b
int **b = new int*[m+1];
for(int p=0;p<=m;p++){
C[p]=new int[n+1]();
b[p]=new int[n+1]();
}
/*对于数组b 4代表“↖”;1代表“↑”,2代表“←”3代表“←↑”
*
*/
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(X[i-1]==Y[j-1]) //X[i]=Y[j]的情况
{
C[i][j]=C[i-1][j-1]+1;
b[i][j]=4; //4代表“↖”
}
else if(C[i-1][j]>C[i][j-1])//C[i-1][j]>C[i][j-1]
{
C[i][j]=C[i-1][j];
b[i][j]=1; //1代表“↑”
}
else if(C[i-1][j]<C[i][j-1])//C[i-1][j]<C[i][j-1]
{
C[i][j]=C[i][j-1];
b[i][j]=2; //2代表“←”
}
else //C[i-1][j]<C[i][j-1]
{
C[i][j]=C[i-1][j];
b[i][j]=3;//3代表“←↑”
}
}
}
cout<<" C: "<<endl;
for(int p=0;p<=m;p++) //输出数组C
{
for(int q=0;q<=n;q++)
{
cout<<C[p][q]<<" ";
}
cout<<endl;
}
cout<<" b: "<<endl;
for(int p=0;p<=m;p++) //输出数组b
{
for(int q=0;q<=n;q++)
{
cout<<b[p][q]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"The LCS is :";
LCS_output(m,n,b); //调用LCS_output函数求出LCS
}
//输出
void AllLcs::LCS_output(int i, int j, int** b)
{
if(i==0||j==0)
{
cout<<endl;
for(int a=store.size()-1;a>=0;a--)
{
cout<<store[a];//输出
}
return;
}
if(b[i][j]==4)
{
store.push_back(X[i-1]); //当X[i]=Y[j]的时候存入
LCS_output(i-1,j-1,b);
store.pop_back();
}
else if(b[i][j]==1)
{
LCS_output(i-1,j,b);}
else if(b[i][j]==2)
{
LCS_output(i,j-1,b);}
else if (b[i][j]==3)
{
LCS_output(i,j-1,b);
LCS_output(i-1,j,b);
}
}
LCS.rar_visual c
版权申诉
168 浏览量
2022-09-14
19:53:01
上传
评论
收藏 1KB RAR 举报
钱亚锋
- 粉丝: 86
- 资源: 1万+
最新资源
- 51单片机学习(1)-软件keil下载
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
- Apache Flink 未授权访问+远程代码执行.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈