每日算法
those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.3.28
P1441 砝码称重
先搜索再dp 学习到了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 25;
const int maxn = 2010;
int a[N] , n , m, tot;
bool vis[N] , f[maxn];
int ans = 0 , res = 0;
void dp()
{
memset(f,0,sizeof f);
f[0] = true;ans = 0;tot = 0;
for(int i = 0;i < n ;i ++)
{
if(vis[i])continue;
for(int j = tot;j >= 0;j --)
{
if(f[j] && !f[j + a[i]])
{
f[j + a[i]] = true;
ans++;
}
}
tot+=a[i];
}
res = max(ans,res);
}
void dfs(int k, int cnt)
{
// 标记 m 个
if(cnt > m)return;
if(k == n)
{
if(cnt == m)dp();
return;
}
dfs(k + 1,cnt);
vis[k] = 1;
dfs(k + 1 , cnt + 1);
vis[k] = 0;
}
int main()
{
cin >> n >> m;
for(int i = 0;i < n ;i ++)
{
scanf("%d",&a[i]);
}
dfs(0 , 0);
cout << res << endl;
return 0;
}
LCS 模板
复习LCS模型
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1005;
char a[N] , b[N];
int f[N][N] , n , m;
int main()
{
cin >> n >> m ;
scanf("%s%s",a + 1,b + 1);
for(int i = 1;i <= n; i++)
{
for(int j = 1;j <= m;j ++)
{
f[i][j] = max(f[i-1][j],f[i][j-1]);
if(a[i] == b[j])
f[i][j] = max(f[i][j],f[i-1][j-1] + 1);
}
}
cout << f[n][m] << endl;
return 0;
}
P1077 摆花
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 105;
const int mod = 1000007;
int a[N] , n , m ;
int f[N][N];
int dfs(int sum ,int now)
{
if(sum > m)return 0;
if(now == n + 1)
{
if(sum == m)return 1;
return 0;
}
if(f[sum][now])return f[sum][now];
int ans = 0;
for(int i = 0;i <= a[now] ;i ++)
{
ans = (ans + dfs(sum + i,now + 1) ) % mod;
}
return f[sum][now] = ans % mod;
}
/*
记忆化搜索都可以转成动态规划
但是动态规划却不一定能转成记忆化搜索
最外层搜索调用时从 1 - n 枚举
*/
void dp()
{
//f[i][j] 前i盆花 摆放了j盆解得集合
f[0][0] = 1;
for(int i = 1;i <= n ;i ++)
{// i表示多少种花
for(int j = 0;j <= m ;j ++)
{ // j 表示多少盆花
for(int k = 0;k <= min(j , a[i]);k++)
{ // k 表示某种花放多少盆
f[i][j] = (f[i][j] + f[i-1][j-k]) % mod;
}
}
}
cout << f[n][m] << endl;
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= n ;i ++)scanf("%d",&a[i]);
//dp();
//cout << dfs(0 , 1) << endl;
return 0;
}