# 求最大子序列长度及最大字段和 --动态规划法C++语言

### 一：最大子序列

1.递归描述：

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int max_csubstr(const string &lhs, const string &rhs)
{
if(lhs.size() == 0 || rhs.size() == 0)
return 0;

if(lhs.back() == rhs.back())
return max_csubstr( lhs.substr(0, lhs.size()-1),
rhs.substr(0, rhs.size()-1) )+ 1;
else
return max( max_csubstr(lhs.substr(0, lhs.size()),
rhs.substr(0, rhs.size()-1)),
max_csubstr(lhs.substr(0, lhs.size()-1),
rhs.substr(0, rhs.size())) );
}

int main()
{
string sz;

while(cin >> sz){
int len = (int)sz.size();
if(len == 1)
cout << len << endl;
else{
string rev_sz(sz);
reverse(rev_sz.begin(), rev_sz.end());
cout << len - max_csubstr(sz, rev_sz) << endl;
}
}

return 0;
}
2.非递归描述：
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX_SIZE 1001

int max_len[MAX_SIZE][MAX_SIZE];

int max_csubstr(const string &lhs, const string &rhs)
{
int len_lhs = (int)lhs.size();
int len_rhs = (int)rhs.size();

memset(max_len, 0, MAX_SIZE * MAX_SIZE);

for(int i=1; i<=len_lhs; ++i){
for(int j=1; j<=len_rhs; ++j){
if(lhs[i-1] == rhs[j-1])
max_len[i][j] = max_len[i-1][j-1] + 1;
else
max_len[i][j] = max(max_len[i-1][j], max_len[i][j-1]);
}
}

return max_len[len_lhs][len_rhs];
}

int main()
{
string sz;

while(cin >> sz){
int len = (int)sz.size();
if(len == 1)
cout << len << endl;
else{
string rev_sz(sz);
reverse(rev_sz.begin(), rev_sz.end());
cout << len - max_csubstr(sz, rev_sz) << endl;
}
}
return 0;
}
3.输出结果:

### 二：最大字段和

11+（-4）+13 =20

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

int find_max_add(vector<int> vec, const int size, int *start, int *end)
{
if(size == 0)
return -1;

int final_max = vec[0];
if(size != 1){
int cur_start = 0;
int cur_end = 0;

for(int i=0; i<size; ++i){
int cur_max = vec[i];
cur_start = i;
for(int j=i+1; j<size; ++j){
cur_max += vec[j];
if(cur_max > final_max){
final_max = cur_max;
*start = cur_start;
*end = j;
}
}
}
}
return final_max;
}

int main()
{
vector<int> vec;
int val;
while(cin >> val && val != -1){
vec.push_back(val);
}

int start = 0;
int end = 0;
int found = find_max_add(vec, vec.size(), &start, &end);
if(found != -1)
cout << start << "->" << end << ":" << found << endl;
else
cout << "input error." << endl;

return 0;
}