一.简单的介绍
动态规划一般用于求最优子结构问题,求全局的解,可以通过求局部的最优解,渐进的达到全局的最优解。
最大公共子串 表示的是字符串str1 和字符串str2 之间存在重复的部分,但是重复的字符串一定要是连续的,不能间断。最大公共子序列表示的是字符串str1 和字符串str2 之间的是存在的字符相等,但是可以不要求是连续的。例如 str1 = "ABCAB"
str2 = "BCBD"。str1和str2之间的最大公共子串是"BC",str1和str2 之间的最大的公共子序列是"BCB".
二.下面举个求最大公共子串的例子:
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
string max_substr(string& str1,string& str2)
{
int len1 = str1.size();
int len2 = str2.size();
vector<int> last_vec(len1+1,0); //上一行
vector<int> cur_vec(len1+1,0); //当前行
int maxlen = 0;
int pos = 0;
for(int i =1;i<= len2;i++) //cdef
{
string s2 = str2.substr(i-1,1);
for(int j = 1; j <= len1;j++)
{
string s1 = str1.substr(j-1,1);
if(s2 == s1)
{
cur_vec[j] = last_vec[j-1] + 1;
if(maxlen < cur_vec[j])
{
maxlen = cur_vec[j];
pos = j;
}
}
}
last_vec = cur_vec;
}
cout << "**pos** :" << pos << "---maxlen--" << maxlen << endl;
string res=str1.substr(pos-maxlen,maxlen);
return res;
}
int main()
{
string str1("abcdef");
string str2("cdef");
string lcs = max_substr(str1,str2);
cout<<lcs<<endl;
return 0;
}
运行的结果是: c d e f
三.看下求最大的子序列的例子(LCS)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MaxLen 50
int GetLcsLength(char* x,char* y,int c[][MaxLen], int b[][MaxLen])
{
int m = strlen(x);
int n = strlen(y);
for(int i = 1;i<= m;i++) //x数组
{
char aa = x[i-1];
for(int j = 1;j <= n;j++) //y数组
{
char bb = y[j-1];
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1] + 1;
b[i][j] = 1; //给出一个标记,有利于后面的查找最大序列
}
else if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 3; //给出一个标记
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = 2; //给出一个标记
}
}
}
return c[m][n]; //返回最大的值
}
void printLcs(int b[MaxLen][MaxLen],char* x,int i ,int j) //传入辅助的数组b 和字符串,从最后一个开始查找
{
if(i==0 || j ==0) //结束条件
return ;
if(b[i][j] == 1) //表示相等的字符
{
printLcs(b,x,i-1,j-1);
cout << " " << x[i-1] << endl;
}
else if(b[i][j] == 2)
{
printLcs(b,x,i,j-1);
}
else
{
printLcs(b,x,i-1,j);
}
}
int main()
{
char y[MaxLen] = {"ABCBDAB"};
char x[MaxLen] = {"BDCABA"};
int b[MaxLen][MaxLen] = {0};
int c[MaxLen][MaxLen] = {0};
int mm = strlen(x);
int nn = strlen(y);
cout << "****result****" << GetLcsLength(x,y,c,b) << endl;
printLcs(b,x,mm,nn);
return 0;
}
运行的结果是:B D A B