【100题】最长公共子串--非连续子串----动态规划

//最长公共子串----动态规划
#include <iostream>
using namespace std;

//定义移动的方向
enum decreaseDir
{
	kInit = 0,
	kLeft,
	kUp,
	kLeftUp
};

int **LCS_length = NULL;

int **LCS_direction = NULL;
//根据生产的方向矩阵,来打印出最大公共子串
void LCS_print
(
	int **LCS_direction,//方向矩阵
	char *p1,
	char *p2,
	size_t row,
	size_t col
)
{
	//指针判空
	if(p1 == NULL || p2 == NULL)
	{
		return ;
	}
	size_t L1 = strlen(p1);
	size_t L2 = strlen(p2);
	
	//异常判断
	if(L1 == 0 || L2 == 0 || !(row < L1 && col < L2))
	{
		return ;
	}

	//左上----就是找到了
	if(LCS_direction[row][col] == kLeftUp)
	{
		if(row>0 && col>0)
		{
			LCS_print(LCS_direction,p1,p2,row-1,col-1);
		}
		cout << p1[row] <<" ";
	}
	//左
	else if(LCS_direction[row][col] == kLeft)
	{
		if(col > 0)
		{
			LCS_print(LCS_direction,p1,p2,row,col-1);
		}
	}
	//上
	else if(LCS_direction[row][col] == kUp)
	{
		if(row > 0)
		{
			LCS_print(LCS_direction,p1,p2,row-1,col);
		}
	}
	else
	{
		cout <<"没有方向可以选择?"<<endl;
	}
}

//计算方向矩阵
int LCS(char *p1, char *p2)
{
	//指针判空
	if(!p1 || !p2)
	{
		return 0;
	}
	size_t L1 = strlen(p1);
	size_t L2 = strlen(p2);
	//长度异常检测
	if(!L1 || !L2)
	{
		return 0;
	}

	size_t i,j;

	//因为事先不知道二维矩阵的维度,所以只能用动态分配,不能用int LCS_length[][]
	//初始化长度矩阵
	
	LCS_length = (int **)(new int[L1]);
	for(i=0; i<L1; ++i)
	{
		LCS_length[i] = (int *)new int[L2];
	}

	for(i=0; i<L1; ++i)
	{
		for(j=0; j<L2; ++j)
		{
			LCS_length[i][j] = 0;
		}
	}

	//初始化方向矩阵

	LCS_direction = (int **)(new int[L1]);
	for(i = 0; i<L1; ++i)
	{
		LCS_direction[i] = (int *)new int[L2];
	}
	for(i=0; i<L1; ++i)
	{
		for(j=0; j<L2; ++j)
		{
			LCS_direction[i][j] = kInit;
		}
	}

	//按照递推表达式,遍历矩阵,赋值。
	for(i=0; i<L1; ++i)
	{
		for(j=0; j<L2; ++j)
		{
			//
			if(i==0 || j==0)
			{
				if(p1[i] == p2[j])
				{
					LCS_length[i][j] = 1;
					LCS_direction[i][j] = kLeftUp;
				}
				else
				{
					LCS_length[i][j] = 0;
				}
			}
			//左上
			else if(p1[i] == p2[j])
			{
				LCS_length[i][j] = LCS_length[i-1][j-1]+1;
				LCS_direction[i][j] = kLeftUp;
			}
			//上
			else if(LCS_length[i-1][j] > LCS_length[i][j-1])
			{
				LCS_length[i][j] = LCS_length[i-1][j];
				LCS_direction[i][j] = kUp;
			}
			//左
			else
			{
				LCS_length[i][j] = LCS_length[i][j-1];
				LCS_direction[i][j] = kLeft;
			}
		}
	}
	//

	LCS_print(LCS_direction,p1,p2,L1-1,L2-1);

	//打印长度矩阵
	cout <<endl<<"长度矩阵:"<<endl;
	for(i=0; i<L1; ++i)
	{
		for(j=0; j<L2; ++j)
		{
			cout << LCS_length[i][j] <<" ";
		}
		cout << endl;
	}
	//打印方向矩阵
	cout << "方向矩阵:"<<endl;
	for(i=0; i<L1; ++i)
	{
		for(j=0; j<L2; ++j)
		{
			cout << LCS_direction[i][j] <<" ";
		}
		cout << endl;
	}

	return LCS_length[L1-1][L2-1];
}

void main()
{
	char p1[] = "BDCABA";
	char p2[] = "ABCBDAB";
	cout <<"最大公共子串的长度为:"<< LCS(p1,p2) <<endl;
}


B C B A
长度矩阵:
0 1 0 1 0 0 1
0 1 1 1 2 2 2
0 1 2 2 2 2 2
1 1 2 2 2 3 3
0 2 2 3 3 3 4
1 2 2 3 3 4 4
方向矩阵:
0 3 0 3 0 0 3
0 2 1 1 3 1 1
0 2 3 1 1 1 1
3 1 2 1 1 3 1
0 3 1 3 1 1 3
3 2 1 2 1 3 1
最大公共子串的长度为:4
请按任意键继续. . .

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值