1.经典应用之一:三角DP
POJ3176
Input
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
输入一个三角形,均为自然数,求从上到下只能向下或者向右下,求一路能得到最大的和
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int a[355][355];
int ans[355][355];
int main(){
int n;
while(cin>>n){
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
for (int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
cin>>a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=i;j++){
ans[i][j]=max(ans[i-1][j-1]+a[i][j],ans[i-1][j]+a[i][j]);//最简单的。。。
}
}
int x=0;
for (int i=1;i<=n;i++){
x=max(x,ans[n][i]);
}
cout<<x<<endl;
}
}
2.经典应用之二:完全背包,求方案总数(简单版)
Time Limit: 2000MS | Memory Limit: 200000K | |
Total Submissions: 15480 | Accepted: 6158 |
Description
1) 1+1+1+1+1+1+1
2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).
Input
Output
Sample Input
7
Sample Output
6
如果将基数换为某些面值不同的硬币,求某个金额的搭配数目,也是一样的解法:
#include<iostream>
#include<cstring>
using namespace std;
int ans[1000005];
int m=1000000000;
int main(){
int n;
while(cin>>n){
memset(ans,0,sizeof(ans));
ans[0]=1;
for (int i=1;i<=n;i*=2){
for (int j=1;j<=n;j++){//注意是由小到大x循环,因为每一种pow(2,n)为无限多个
if (j>=i) ans[j] = (ans[j]+ans[j-i])%m;//每都一种面额就累加
}
}
cout<<ans[n]<<endl;
}
}
3.经典应用之三:01背包,求最优解:
poj3616
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7067 | Accepted: 2945 |
Description
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.
Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.
Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.
Input
* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers:
starting_houri , ending_houri , and
efficiencyi
Output
* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours
Sample Input
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
Sample Output
43
将每段按结束时间排序,然后ans[i]表示从开始到i能得到的最大值,ans[i] = max (ans[i],ans[j]+a[j].val) for j =0...i,且a[j].end<a[i].start;复杂度为O(n2);
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct node{
int sta;
int end;
int val;
node (){
sta=end=val=0;
}
bool operator < (const node &b) const{
if (end!=b.end){
return end<b.end;
}
else return sta<b.sta;
}
};
int main(){
int n,m,r;
while(cin>>n>>m>>r){
node a[m];
int ans[1001]={0};
for (int i=0;i<m;i++){
cin>>a[i].sta>>a[i].end>>a[i].val;
a[i].end+=r;
}
sort(a,a+m);
for (int i=0;i<m;i++){
ans[i]=max(ans[i],a[i].val);
for (int j=0;j<i;j++){
if (a[j].end<=a[i].sta){
ans[i]=max(ans[i],ans[j]+a[i].val);
}
}
}
int t=0;
for (int i=0;i<m;i++){
t=max(ans[i],t);
}
cout<<t<<endl;
}
}
4.经典应用之三:最长回文子串:
给出一个字符串,判断最长回文子串:
int lpsDp(char * str,int n){
int dp[n][n], tmp;
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++) dp[i][i] = 1;
// i 表示 当前长度为 i+1的 子序列
for(int i=1; i<n; i++){
tmp = 0;
//考虑所有连续的长度为i+1的子串. 该串为 str[j, j+i]
for(int j=0; j+i<n; j++){
//如果首尾相同
if(str[j] == str[j+i]){
tmp = dp[j+1][j+i-1] + 2;
}else{
tmp = max(dp[j+1][j+i],dp[j][j+i-1]);
}
dp[j][j+i] = tmp;
}
}
//返回串 str[0][n-1] 的结果
return dp[0][n-1];
}
5.最长递增子序列,前面已经写过优化的nlogn的算法,所以不再叙述
未完待续。。。。