最长公共子序列
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;
}
#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;
}
相关文章推荐
- 数学相关简单习题练习
- Yii Framework 开发教程(10) UI 组件 自定义组件
- 数据库完整性问题
- OmniThreadLibrary 3.03b发布了
- 不小心删掉的照片怎么找回
- BIND9 +LDZ+MYSQL构建企业DNS智能解析
- 套接字编程简介<二>
- 什么是错版币
- Yii Framework 开发教程(9) UI 小组件Widget 概述
- Asp.net:清空一段文字内的所有html标签
- 573 - The Snail
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
- 在UBUNTU 13.10 搭建PYTHON开发环境
- Android Web Server
- Linux中后缀名为ko、o、a、so、la的文件介绍
- 统计n个随机数(随机0~10),每出现一次就用*号在数字下面写一次*【c语言实现】
- ubuntu 13.10搭建Android环境
- Yii Framework 开发教程(8) 使用FormModel
- 77 Android BroadcastReceiver 和 Notification
- 矩阵链乘法