动态规划一
- 前言
- 数字三角形 The Triangle([poj1163](http://poj.org/problem?id=1163))
- 最长上升子序列LIS([百练2757](http://bailian.openjudge.cn/practice/2757/))
- 最长公共子序列LCS([poj458](http://poj.org/problem?id=1458))
前言
MOOK程序设计与算法(二)算法基础-郭伟-----学习笔记
数字三角形 The Triangle(poj1163)
Description
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
题目大意:求从上到下最长的路径,没次可以沿对角线向左或向右走;
递归解法
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Max = 105;
int N = 0;
int a[105][105];
int tem[Max][Max];
int max_d(int i,int j){
if(tem[i][j]!=0) return tem[i][j];
if(i==N) return a[i][j];
return tem[i][j]=max(max_d(i+1,j),max_d(i+1,j+1))+a[i][j];
}
int main(){
cin>>N;
for(int i = 1;i<=N;i++){
for(int j = 1;j<=i;j++){
cin>>a[i][j];
}
}
cout<<max_d(1,1);
return 0;
}
递推解法
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int Max = 105;
int N = 0;
int a[105][105];
//int max_d(int i,int j){
// if(tem[i][j]!=0) return tem[i][j];
// if(i==N) return a[i][j];
// return tem[i][j]=max(max_d(i+1,j),max_d(i+1,j+1))+a[i][j];
//}
int main(){
cin>>N;
for(int i = 1;i<=N;i++){
for(int j = 1;j<=i;j++){
cin>>a[i][j];
}
}
for(int i = N-1;i>=1;i--){
for(int j = 1;j<=i;j++){
a[N][j] = max(a[N][j],a[N][j+1])+a[i][j];
}
}
cout<<a[N][1];
return 0;
}
最长上升子序列LIS(百练2757)
题目:
一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, …, aN),我们可以得到一些上升的子序列(ai1, ai2, …, aiK),这里1 <= i1 < i2 < … < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5, 8).
你的任务,就是对于给定的序列,求出最长上升子序列的长度。
题解
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int n = 10005;
int a[n];
int maxLen[n];
int main(){
int N = 0;
cin>>N;
for(int i = 1;i<=N;i++){
cin>>a[i];
maxLen[i] = 1;
}
for(int i = 2;i<=N;i++ ){
for(int j = 1;j<=i;j++){
if(a[j]<a[i])
maxLen[i] = max(maxLen[i],maxLen[j]+1);
}
}
cout<<*max_element(maxLen+1,maxLen+N+1);
return 0;
}
最长公共子序列LCS(poj458)
题目:
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
个人题解
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
string str1,str2;
int dp[1000][1000];
int main(){
while(cin>>str1>>str2){
memset(dp,0,sizeof(dp));
for(int i = 1;i<=str1.length();i++){
for(int j = 1;j<=str2.length();j++){
if(str1[i-1]==str2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
cout<< dp[str1.length()][str2.length()]<<endl;
}
return 0;
}//自己测都没问题,提交后都编译错误