您的位置:首页 > 其它

最长公共子序列

2014-01-18 16:53 232 查看
程序有错误,仅供本人阅览

#include "stdafx.h"

#include<iostream>

#include<string>

using namespace std;

void LCSLength(string x,string y, int(*c)[100],int(*b)[100])

{

int m=x.length();

int n=y.length();

for(int i=1;i<=m;i++)

c[i][0]=0;

for(int j=0;j<=n;j++)

c[0][j]=0;

for(int i=1;i<=m;i++)

{

for(int j=1;j<=n;j++)

{

if (x[i-1]==y[j-1])

{

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 printlcs(string x,int (*b)[100],int i,int j)

{

if(i==0||j==0)

{

return;

}

else

{

if(b[i][j]==1)

{

printlcs(x,b,i-1,j-1);

cout<<x[i-1]<<" ";

}

else if(b[i][j]==2)

{

printlcs(x,b,i-1,j);

}

else

{

printlcs(x,b,i,j-1);

}

}

}

int _tmain(int argc, _TCHAR* argv[])

{

string x="ABCBDAB";

string y="BDCABA";

int (*m)[100]={0};

int (*n)[100]={0};

LCSLength(x,y,m,n);

printlcs(x,n,x.length(),y.length());

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: