射箭解题报告
带权值的最长公共子序列
相当于用箭和怪建矩阵
在g[i][k]上找最长公共子序列
-2 -4 4 3 2
-1 -3 5 4 3
-7 -9 -1 -2 -3
0 -2 6 5 4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
f[i][k]=max(f[i][k]+g[i][k], f[i-1][k], f[i][k-1])
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #define maxn 3001 int jian[maxn], guai[maxn], f[maxn][maxn]; int max(int a, int b, int c) { if(b>a) a=b; if(c>a) a=c; return a; } int main() { int n, m, i, k; scanf("%d%d", &n, &m); for(i=1; i<=n; i++) scanf("%d", &jian[i]); for(i=1; i<=m; i++) scanf("%d", &guai[i]); for(i=1; i<=n; i++) { for(k=1; k<=m; k++) { f[i][k]=max(f[i-1][k-1]+jian[i]-guai[k], f[i-1][k], f[i][k-1]); } } printf("%d", f[n][m]); return 0; }
最长公共子序列的动规方程
f[i][j]=max(f[i-1][j-1]+(x[i]==y[j]), f[i][j-1], f[i-1][j]);
其实这道题就是带了权值
f[i][k]=max(f[i-1][k-1]+jian[i]-guai[k], f[i-1][k], f[i][k-1]);