最长公共子序列(LCS)的C++实现
2017-07-26 22:09
363 查看
首先有如下递归式
绘制图表如下
首先我们实现最长公共子序列长度的输出,代码如下:
#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]
<< 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]
<< 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);
}
}
绘制图表如下
首先我们实现最长公共子序列长度的输出,代码如下:
#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]
<< 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]
<< 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);
}
}
相关文章推荐
- 算法学习 - 最长公共子序列(LCS)C++实现
- C++实现最长公共子序列LCS问题
- LCS最长公共子序列C++代码实现
- 最长公共子序列LCS(C++实现)
- Python 实现最长公共子序列LCS
- 动态规划实现最长公共子序列(LCS)算法
- 最长公共子序列的C++实现---附二维指针的使用方法
- C++实现——LCS-最大公共子串长度
- 最长公共子序列(LCS) c语言/c++
- 算法学习心得——动态规划法实现最长公共子序列(LCS)
- 动态规划之最长公共子序列问题 C++实现
- 算法导论中LCS的C++实现程序
- 两个链表的公共区域以及LCS(最长公共子序列)实现
- 算法导论-第15章-动态规划:最长公共子序列(空间复杂度改进版)C++实现
- Java实现算法导论中最长公共子序列(LCS)动态规划法
- LCS问题(最长公共子序列)-动态规划实现
- C++ 动态规划-LCS最长公共子序列
- 利用C++实现最长公共子序列与最长公共子串
- 基于动态规划的最长公共子序列实现(LCS)
- LCS问题(最长公共子序列)-动态规划实现