动态规划一
前言
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;
}//自己测都没问题,提交后都编译错误
0/1 背包【hdu 2602 “Bone Collector”】
背包问题:有多个物品,重量不同、价值不同吗,以及一个容量有限的背包,选择一些物品中到背包中,问怎么才能使装进背包的物品总价值最大。
(1):如果每个物品可分,称为一般背包问题,用贪心法求最优解;
(2):如果每个物品不可分割称为0/1背包问题。
“骨头收集者”带着体积为V的背包去捡骨头,已知每个骨头的体积和价值,求能装进背包的最大价值。N<=1000,V<=1000;
输入:第1行是测试数量,第二行是骨头数量和背包的体积,第三行是每个骨头的价值,第四行是每个骨头的体积。
1
5 10
1 2 3 4 5
5 4 3 2 1
题解
#define mytest
#include<bits/stdc++.h>
using namespace std;
struct BONE{
int value;
int volume;
}bone[1011];
int N , V;
int dp[1011][1011];
int ans() {
memset(dp,0,sizeof(dp));
for(int i = 1;i<=N;i++){
for(int j = 0;j<=V;j++){
if(bone[i].volume>j)dp[i][j] = dp[i-1][j];
else dp[i][j] = max(dp[i-1][j],dp[i-1][j-bone[i].volume]+bone[i].value);
}
}
return dp[N][V];
}
int main() {
#ifdef mytest
freopen("C:\\Users\\刘杰\\Desktop\\HTML\\text.in.txt", "r", stdin);
#endif
cin >> N >> V;
for (int i = 1; i <= N; ++i) {
cin >> bone[i].value;
}
for (int i = 1; i <= N; i++) {
cin >> bone[i].volume;
}
cout << ans() << endl;
return 0;
}