A - Number Triangle
Topic link: 22 Algorithm Design and Analysis - Dynamic Programming - Virtual Judge
Analysis: Let f[i][j] represent the maximum value that can be reached to the jth position of the i-th row, which can be updated from bottom to top or traversed from top to bottom. What I use here is bottom-up For traversal, first set f[n][i]=a[n][i], that is, assign the initial value of the bottom f array, and then use f[i][j]=max(f[i+ 1][j],f[i+1][j+1])+a[i][j] to update. The final result exists in f[1][1]
Here is the code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1003;
int f[N][N],a[N][N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
f[n][i]=a[n][i];
for(int i=n-1;i>=1;i--)
for(int j=1;j<=i;j++)
f[i][j]=max(f[i+1][j],f[i+1][j+1])+a[i][j];
cout<<f[1][1];
return 0;
}
B - common subsequence
(POJ - 1458) Common Subsequence (longest common subsequence) - AC__dream's blog - CSDN blog
C - Palindromic substring
Palindromic substring (interval dynamic programming) - AC__dream's blog - CSDN blog
D - Brackets problem
Topic link: 22 Algorithm Design and Analysis - Dynamic Programming - Virtual Judge
Analysis: A typical interval problem, let f[i][j] represent the maximum length that can be matched by brackets from i~j, and then we discuss the left and right endpoints of the interval when traversing the interval, if s[i]==s[j], Then f[i][j]=f[i+1][j-1]+2, add two matching brackets of the current left and right endpoints to the maximum bracket matching length of i+1~j-1, If it is not satisfied, go directly to the breaking point to discuss the maximum value.
Here is the code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=5e2+10;
int f[N][N];
int main()
{
string s;
while(cin>>s)
{
if(s[0]=='e') break;
memset(f,0,sizeof f);
int l=s.length();
for(int len=2;len<=l;len++)
for(int i=0;i+len-1<l;i++)
{
int j=i+len-1;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']')) f[i][j]=f[i+1][j-1]+2;
for(int k=i;k<j;k++)
f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]);
}
printf("%d\n",f[0][l-1]);
}
return 0;
}
E - Merge stones
P1880 [NOI1995] Stone Merge (Interval DP+ Thinking)_AC__dream's Blog - CSDN Blog
F - Hay for Sale
Topic link: 22 Algorithm Design and Analysis - Dynamic Programming - Virtual Judge
This is a basic 01 backpack problem, put the code directly
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=5e4+10;
int f[N],v[N];
int main()
{
int V,n;
cin>>V>>n;
for(int i=1;i<=n;i++)
scanf("%d",&v[i]);
for(int i=1;i<=n;i++)
for(int j=V;j>=v[i];j--)
f[j]=max(f[j],f[j-v[i]]+v[i]);
printf("%d",f[V]);
return 0;
}
G - Favorite recursion
Topic link: 22 Algorithm Design and Analysis - Dynamic Programming - Virtual Judge
It is not recommended to use memoized search to solve this, because when using memoized search, once the boundary problem is not solved well, it will time out. You can directly perform dynamic transfer according to the conditions given in the title, because it is completely in accordance with the method given in the title. Transferred, so just put the code directly
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=55;
long long dp[N][N][N];
int main()
{
long long a,b,c;
for(int i=0;i<=20;i++)
for(int j=0;j<=20;j++)
for(int k=0;k<=20;k++)
{
if(i==0||j==0||k==0)
dp[i][j][k]=1;
else if(i<j&&j<k)
dp[i][j][k]=dp[i][j][k-1]+dp[i][j-1][k-1]-dp[i][j-1][k];
else
dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k]+dp[i-1][j][k-1]-dp[i-1][j-1][k-1];
}
while(1)
{
scanf("%lld%lld%lld",&a,&b,&c);
if(a==-1&&b==-1&&c==-1) return 0;
if(a<=0||b<=0||c<=0)
printf("w(%lld, %lld, %lld) = 1\n",a,b,c);
else if(a>20||b>20||c>20)
printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,dp[20][20][20]);
else
printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,dp[a][b][c]);
}
return 0;
}
H - ATM
(POJ-1276) Cash Machine (Binary Optimization + Multiple Backpacks) - AC__dream's Blog - CSDN Blog
I - Sakura Sakura
(P1445) [Violet] Cherry Blossoms (Prime Factorization) - AC__dream's Blog - CSDN Blog
J - another palindrome
(POJ3280) Cheapest Palindrome (Interval DP) - AC__dream's Blog - CSDN Blog