求两个字符串的最长公共子序列

 

具体的算法思想参考以下文章:

http://blog.csdn.net/lisonglisonglisong/article/details/41548557

http://blog.csdn.net/zhongkeli/article/details/8847694

https://www.kancloud.cn/digest/pieces-algorithm/163624

 

 

#include "iostream"
#include "string"
#include "vector"
using namespace std;

std::vector<char> common;
int lcs(string A, string B) {
	std::vector<vector<int> > len;
	len.resize(A.size() + 1);
	for (int i = 0; i <= static_cast<int>(A.size()); i++) {
		len[i].resize(B.size() + 1, 0);
	}
	for (int i = 1; i <= static_cast<int>(A.size()); ++i)
	{
		for (int j = 1; j <= static_cast<int>(B.size()); ++j)
		{
			if (A[i - 1] == B[j - 1])
			{
				len[i][j] = len[i - 1][j - 1] + 1;
			}
			else if (len[i - 1][j] >= len[i][j - 1])
			{
				len[i][j] = len[i - 1][j];
			}
			else {
				len[i][j] = len[i][j - 1];
			}
		}
	}
	int apos = A.size();
	int bpos = B.size();
	int commonlen = len[apos][bpos];
	int k = commonlen;
	common.resize(commonlen);
	while (apos && bpos) {
		if (len[apos][bpos] == len[apos - 1][bpos-1] + 1) {
			common[--k] = A[--apos];
			--bpos;
		}
		else if (len[apos - 1][bpos] >= len[apos][bpos - 1])
		{
			--apos;
		}
		else {
			--bpos;
		}
	}
	for (int i = 0; i < commonlen; i++) {
		cout << common[i];
	}
	cout << endl;
	return commonlen;
}


int main()
{
	string A = "aQ11";
	string B = "a11df";
	cout << lcs(A, B);

	system("pause");
	return 0;
}

动态规划解决LCS问题的时间复杂度为O(mn),这比简单的递归实现要快多了。空间复杂度是O(mn),因为使用了一个动态规划表。

 

要输出所有LCS的内容

两个字符串对应的最长公共子序列不一定唯一,这个程序输出所有的LCS内容。

基本思想是:

 

具体参考文章:http://blog.csdn.net/lisonglisonglisong/article/details/41596309

#include <vector>
#include <iomanip>
#include <set>
#include <string>
#include <map>
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

set<string> all_lcs; //注意这里要用set去除重复的LCS
//最长公共子串(LCS)
//二维数组veca[i][j]记录的是两个字符串Xi和Yj的LCS长度
int LCS_length(const string &str1, const string &str2, vector<vector<int> > &veca) {
	int i, j;
	int biggest = 0;
	if (str1 == "" || str2 == "")
		return 0;

	for (i = 0; i <= str1.length(); i++) {
		veca[i][0] = 0;
	}
	for (j = 0; j <= str2.length(); j++) {
		veca[0][j] = 0;
	}
	for (i = 1; i <= str1.length(); i++) {
		for (j = 1; j <= str2.length(); j++) {
			if (str1[i - 1] == str2[j - 1]) {
				veca[i][j] = veca[i - 1][j - 1] + 1;
			}
			else {
				if (veca[i - 1][j] >= veca[i][j - 1])
					veca[i][j] = veca[i - 1][j];
				else
					veca[i][j] = veca[i][j-1];
			}
		}
	}
	return veca[str1.length()][str2.length()];
}

//该函数找出所有的LCS的序列,并将其存在vector中
void PrintAllLCS(string &str1, string &str2, int i, int j, 
	             vector<vector<int> > &veca, string lcs_str) {
//注意这里形参lcs_str不可以为引用,这里需要每次调用lcs_str都重新生成一个对象
	while (i > 0 && j > 0) {
		if (str1[i - 1] == str2[j - 1]) {
			lcs_str = str1[i - 1] + lcs_str; //逆向存放
			--i;
			--j;
		}
		else {
			if (veca[i - 1][j] > veca[i][j - 1]) //向左走
				--i;
			else if (veca[i - 1][j] < veca[i][j - 1]) //向上走
				--j;
			else { //此时向上向右均为LCS的元素
				PrintAllLCS(str1, str2, i - 1, j, veca, lcs_str);
				PrintAllLCS(str1, str2, i, j - 1, veca, lcs_str);
				return;
			}
		}
	}
	cout << "   " << lcs_str << endl;
	all_lcs.insert(lcs_str);
}
int main() {
	string input;
	getline(cin, input);
	stringstream ss(input);
	string str1, str2;
	ss >> str1;
	ss >> str2;
	//将veca初始化为一个二维数组,其行列值分别为str1和str2的长度加1
	//二维数组veca记录的是两个字符串Xi和Yj的LCS长度
	vector<vector<int> > veca(str1.length() + 1, vector<int>(str2.length() + 1));
	cout << LCS_length(str1, str2, veca) << endl;

	string lcs_str;
	PrintAllLCS(str1, str2, str1.length(), str2.length(), veca, lcs_str);
	set<string>::iterator iter = all_lcs.begin();
	while (iter != all_lcs.end()) {
		cout << *iter++ << endl;
	}
	return 0;
}

如图所示的两个字符串共有三个LCS。

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值