<pre name="code" class="cpp">#include <bits/stdc++.h>
using namespace std;
#define maxn 105
int w[maxn], v[maxn], dp[maxn][maxn * maxn];
int W, n;
int main()
{
while(cin>>n>>W)
{
memset(dp, 0, sizeof(dp));
for(int i=0; i<n; i++)
cin>>w[i]>>v[i];
for(int i=0; i<n; i++)
for(int j=0; j<=W; j++)
if(j >= w[i]) dp[i + 1][j] = max(dp[i][j] , dp[i][j - w[i]] + v[i]);
else dp[i + 1][j] = dp[i][j];
cout<<dp[n][W]<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define maxn 105
int n, W, w[maxn], v[maxn], dp[maxn][maxn * maxn];
int solve(int i, int j)
{
if(dp[i][j] > 0) return dp[i][j];
if(i == n) return 0;
for(int k=0; k<=W; k++)
if(k >= w[i]) dp[i][j] = max(solve(i+1 , j - w[i]) + v[i], solve(i+1 , j));
else dp[i][j] = solve(i+1, j);
return dp[i][j];
}
int solve(int x, int y)
{
if(dp[x][y] > 0) return dp[x][y];
if(x == n) return dp[x][y] = 0;
if(y >= w[x]) dp[x][y] = max(solve(x+1, y-w[x])+v[x] , solve(x+1 , y));
return dp[x][y];
}
int main()
{
while(cin>>n>>W)
{
for(int i=0; i<n; i++)
cin>>w[i]>>v[i];
solve(0, W);///(0,W)表示还有0个可以选
cout<<dp[0][W]<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define maxn 1000+5
string s1, s2;
int dp[maxn][maxn];
int solve(int i, int j)
{
if()
if(s1[i] == s2[j]) dp[i][j] =
}
int main()
{
while(cin>>s1>>s2)
{
memset(dp, 0, sizeof(dp));
int len1 = s1.size();
int len2 = s2.size();
for(int i=0; i<len1; i++)
for(int j=0; j<len2; j++)
if(s1[i] == s2[j]) dp[i+1][j+1] = dp[i][j] + 1;
else
{
dp[i+1][j+1] = max(dp[i][j+1] , dp[i+1][j]);
}
cout<<dp[len1][len2]<<endl;
}
return 0;
}
动态规划小练习
最新推荐文章于 2024-05-23 16:47:51 发布