目录
1、线性dp
1.1、数字三角形
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510, INF = 1e9;
int n;
//每个点
int a[N][N];
//转态
int f[N][N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
scanf("%d", &a[i][j]);
//处理边界,将所有的f(状态)初始化成负无穷。
//因为有负边 所以初始化为负无穷。
for (int i = 0; i <= n; i ++ )
for (int j = 0; j <= i + 1; j ++ )
f[i][j] = -INF;
f[1][1] = a[1][1];
for (int i = 2; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
f[i][j] = max(f[i - 1][j - 1] + a[i][j], f[i - 1][j] + a[i][j]);
int res = -INF;
//求底层的最大值
for (int i = 1; i <= n; i ++ ) res = max(res, f[n][i]);
printf("%d\n", res);
return 0;
}
转移计算量 本题o(n^2)*o(1) =25万
1.2、最长上升子序列
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for (int i = 1; i <= n; i ++ )
{
//状态计算时 当j从0到i-1时 且(f[j]>f[i])有
//f[i]=max(f[i],f[j]+1)
//f[i]=1 是当j=0时的表达
f[i] = 1; // 只有a[i]一个数,给每次计算赋初值
for (int j = 1; j < i; j ++ )
if (a[j] < a[i])//是否满足上升子序列要求
f[i] = max(f[i], f[j] + 1);
}
int res = 0;
for (int i = 1; i <= n; i ++ ) res = max(res, f[i]);
printf("%d\n", res);
return 0;
}
保存并且输出最长序列
1.3、最长上升子序列2
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n;
int a[N];
//所有不同上升子序列结尾的最小值
int q[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
//q中的元素个数
int len = 0;
//通过二分枚举出来小于某个数的最大数
for (int i = 0; i < n; i ++ )
{
int l = 0, r = len;
while (l < r)
{
int mid = l + r + 1 >> 1;
//check函数
if (q[mid] < a[i]) l = mid;
else r = mid - 1;
}
//r表示可以接到哪个长度后边
len = max(len, r + 1);
//之前的q[r+1]>=a[i] q[r]<a[i] 保持序列的单调性。重新赋值。
q[r + 1] = a[i];
}
printf("%d\n", len);
return 0;
}
1.4、最长公共子序列
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
char a[N], b[N];
int f[N][N];
int main()
{
scanf("%d%d", &n, &m);
//因为使用了i-1和j-1,下标最好从1开始
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]);
//仅当相等的时候,a[i],b[j]同时出现在子序列中,且为子序列的最后一个元素。
if (a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
}
//在a前n个字母且在b的前m个字母中出现的子序列的最大值
printf("%d\n", f[n][m]);
return 0;
}
f[i,j]的最大值为这4个状态中的最大值
1.5、最短编辑距离
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
char a[N], b[N];
int f[N][N];
int main()
{
//涉及到i-1的时候,下标从1开始,以防边界值判断
scanf("%d%s", &n, a + 1);
scanf("%d%s", &m, b + 1);
//对边界值赋值
//a的前0个字母匹配整个b的时候 需要添加。则b有几个字母就加几次。
for (int i = 0; i <= m; i ++ ) f[0][i] = i;
//把a的前i个字母与b的前0个字母匹配 删除 a有多少个就删多少次。
for (int i = 0; i <= n; i ++ ) f[i][0] = i;
//下标为0已经计算完毕,从1开始即可
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
{
f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1);
}
//把a的前n个字母,变成个b的前m个字母
printf("%d\n", f[n][m]);
return 0;
}
1.6、编辑距离
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 15, M = 1010;
int n, m;
//每个字符串 长度只有15
int f[N][N];
//1000个字符串,每个字符串最大长度15
char str[M][N];
//因为传入的时字符串 需要是a[] b[]
int edit_distance(char a[], char b[])
{
//因为a[0]没读入 需要从a[1]开始记录长度
int la = strlen(a + 1), lb = strlen(b + 1);
for (int i = 0; i <= lb; i ++ ) f[0][i] = i;
for (int i = 0; i <= la; i ++ ) f[i][0] = i;
for (int i = 1; i <= la; i ++ )
for (int j = 1; j <= lb; j ++ )
{
f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
f[i][j] = min(f[i][j], f[i - 1][j - 1] + (a[i] != b[j]));
}
return f[la][lb];
}
int main()
{
scanf("%d%d", &n, &m);
//下标从1开始
for (int i = 0; i < n; i ++ ) scanf("%s", str[i] + 1);
while (m -- )
{
char s[N];
int limit;
//下标从1开始
scanf("%s%d", s + 1, &limit);
int res = 0;
for (int i = 0; i < n; i ++ )
//所有的字符串与s
if (edit_distance(str[i], s) <= limit)
res ++ ;
printf("%d\n", res);
}
return 0;
}
2、区间dp
2.1、石子合并
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
int n;
int s[N];
int f[N][N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &s[i]);
//区间和
for (int i = 1; i <= n; i ++ ) s[i] += s[i - 1];
//按照长度从小到大,枚举所由区间长度
//len=1时,合并所需要代价为0.由于时全局变量已经赋值
for (int len = 2; len <= n; len ++ )
//枚举所有的起点 i+len-1最后一个位置
for (int i = 1; i + len - 1 <= n; i ++ )
{
//左右端点
int l = i, r = i + len - 1;
//f[l][r]初值是0,求min 所以要为它赋一个大数
f[l][r] = 1e8;
//枚举分界点 k<r 意味着右边至少一个
for (int k = l; k < r; k ++ )
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
}
//将第1堆到第n堆合并的最小代价
printf("%d\n", f[1][n]);
return 0;
}
3、计数类dp
3.1、整数划分
完全背包解法
状态表示:
f[i][j]表示只从1~i中选,且总和等于j的方案数
状态转移方程:
f[i][j] = f[i - 1][j] + f[i][j - i];
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010, mod = 1e9 + 7;
int n;
int f[N];
int main()
{
cin >> n;
//一个数都不选。只有f[0]初始化为1
//f[1],f[2],在一个都不选的时候不可能得到,由全局变量赋值为0即可。
f[0] = 1;
for (int i = 1; i <= n; i ++ )
for (int j = i; j <= n; j ++ )
f[j] = (f[j] + f[j - i]) % mod;
cout << f[n] << endl;
return 0;
}
其他算法
状态表示:
f[i][j]表示总和为i,总个数为j的方案数
状态转移方程:
f[i][j] = f[i - 1][j - 1] + f[i - j][j];
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010, mod = 1e9 + 7;
int n;
int f[N][N];
int main()
{
cin >> n;
//初始化
f[1][1] = 1;
for (int i = 2; i <= n; i ++ )
//j个数字,最多就是i个1
for (int j = 1; j <= i; j ++ )
f[i][j] = (f[i - 1][j - 1] + f[i - j][j]) % mod;
int res = 0;
for (int i = 1; i <= n; i ++ ) res = (res + f[n][i]) % mod;
cout << res << endl;
return 0;
}
完全背包 体积从小到大循环
其他算法
两种算法对比。最后转态转移方程其实是一致的