C++实现最长公共子序列和最长公共子串

转载自:点击打开链接

C++实现最长公共子序列和最长公共子串_第1张图片


// LCS.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 

using namespace::std;

int lcs(string str1, string str2, vector>& vec) {
	int len1 = str1.size();
	int len2 = str2.size();
	vector> c(len1 + 1, vector(len2 + 1, 0));
	for (int i = 0; i <= len1; i++) {
		for (int j = 0; j <= len2; j++) {
			if (i == 0 || j == 0) {
				c[i][j] = 0;
			}
			else if (str1[i - 1] == str2[j - 1]) {
				c[i][j] = c[i - 1][j - 1] + 1;
				vec[i][j] = 0;
			}
			else if (c[i - 1][j] >= c[i][j - 1]){
				c[i][j] = c[i - 1][j];
				vec[i][j] = 1;
			}
			else{
				c[i][j] = c[i][j - 1];
				vec[i][j] = 2;
			}
		}
	}

	return c[len1][len2];
}

void print_lcs(vector>& vec, string str, int i, int j)
{
	if (i == 0 || j == 0)
	{
		return;
	}
	if (vec[i][j] == 0)
	{
		print_lcs(vec, str, i - 1, j - 1);
		printf("%c", str[i - 1]);
	}
	else if (vec[i][j] == 1)
	{
		print_lcs(vec, str, i - 1, j);
	}
	else
	{
		print_lcs(vec, str, i, j - 1);
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	string str1 = "123456";
	string str2 = "2456";
	vector> vec(str1.size() + 1, vector(str2.size() + 1, -1));
	int result = lcs(str1, str2, vec);

	cout << "result = " << result << endl;

	print_lcs(vec, str1, str1.size(), str2.size());

	getchar();
	return 0;
}



C++实现最长公共子序列和最长公共子串_第2张图片


int lcs_2(string str1, string str2, vector>& vec) {
	int len1 = str1.size();
	int len2 = str2.size();
	int result = 0;     //记录最长公共子串长度
	vector> c(len1 + 1, vector(len2 + 1, 0));
	for (int i = 0; i <= len1; i++) {
		for (int j = 0; j <= len2; j++) {
			if (i == 0 || j == 0) {
				c[i][j] = 0;
			}
			else if (str1[i - 1] == str2[j - 1]) {
				c[i][j] = c[i - 1][j - 1] + 1;
				vec[i][j] = 0;
				result = c[i][j] > result ? c[i][j] : result;
			}
			else {
				c[i][j] = 0;
			}
		}
	}
	return result;
}

void print_lcs(vector>& vec, string str, int i, int j)
{
	if (i == 0 || j == 0)
	{
		return;
	}
	if (vec[i][j] == 0)
	{
		print_lcs(vec, str, i - 1, j - 1);
		printf("%c", str[i - 1]);
	}
	else if (vec[i][j] == 1)
	{
		print_lcs(vec, str, i - 1, j);
	}
	else
	{
		print_lcs(vec, str, i, j - 1);
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	string str1 = "123456";
	string str2 = "14568";
	vector> vec(str1.size() + 1, vector(str2.size() + 1, -1));
	int result = lcs_2(str1, str2, vec);

	cout << "result = " << result << endl;

	print_lcs(vec, str1, str1.size(), str2.size());

	getchar();
	return 0;
}


你可能感兴趣的:(数据结构与算法)