1. 程式人生 > >最長公共子序列(LCS)的C++實現

最長公共子序列(LCS)的C++實現

首先有如下遞迴式

繪製圖表如下

首先我們實現最長公共子序列長度的輸出,程式碼如下:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int table[100][100] = { 0 };
int main()
{
	string a, b;
	cin >> a >> b;
	int m = a.length();
	int n = b.length();
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (a[i-1] == b[j-1])
			{
				table[i][j] = table[i - 1][j - 1] + 1;
			}
			else
			{
				table[i][j] = max(table[i - 1][j], table[i][j - 1]);
			}
		}
	}
	cout << table[m][n] << endl;
	system("pause");
}
只需做一點小改動,假如一個判斷陣列和一個輸出函式,用到遞迴,程式碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int table[100][100] = { 0 };
int judge[100][100] = { 0 };
int print_LCS(int judge[][100], string x,int,int);
int main()
{
	string a, b;
	cin >> a >> b;
	int m = a.length();
	int n = b.length();
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (a[i-1] == b[j-1])
			{
				table[i][j] = table[i - 1][j - 1] + 1;
				judge[i][j] = 1;
			}
			else if(table[i - 1][j] >= table[i][j - 1])
			{
				table[i][j] = table[i - 1][j];
				judge[i][j] = 2;
			}
			else
			{
				table[i][j] = table[i][j - 1];
				judge[i][j] = 3;
			}
		}
	}
	cout << table[m][n] << endl;
	print_LCS(judge, a, m, n);
	cout << endl;
	system("pause");
}
int print_LCS(int judge[][100],string a,int x,int y)//x,y,分別為兩段長度
{
	if (x == 0 || y == 0)
	{
		return 0;
	}
	if (judge[x][y] == 1)
	{
		print_LCS(judge, a, x-1, y-1);
		cout << a[x-1];
	}
	if (judge[x][y] == 2)
	{
		print_LCS(judge, a, x - 1, y);
	}
	if (judge[x][y] == 3)
	{
		print_LCS(judge, a, x, y - 1);
	}
}