hdu 1243 反恐训练营(dp 最大公共子序列变形)

时间:2024-10-17 13:08:02

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1243

d[i][j] 代表第i 个字符与第 j 个字符的最大的得分。,,

最大公共子序列变形

 #include <cstring>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = +; char s[maxn], x[maxn], y[maxn];
int f[], val[maxn], d[maxn][maxn];
int main()
{
int n, i, j;
int sum;
while(~scanf("%d", &n))
{
sum = ;
scanf("%s", s);
for(i = ; i < n; i++)
scanf("%d", &val[i]);
for(i = ; i < strlen(s); i++)
f[s[i]] = val[i];
scanf("%s%s", x, y);
int len1 = strlen(x);
int len2 = strlen(y);
for(i = ; i <= len1; i++)
d[i][] = ;
for(i = ; i <= len2; i++)
d[][i] = ;
for(i = ; i <= len1; i++)
for(j = ; j <= len2; j++)
if(x[i-]==y[j-])
{
d[i][j] = d[i-][j-] + f[x[i-]];
}
else
{
if(d[i-][j] > d[i][j-])
d[i][j] = d[i-][j];
else
d[i][j] = d[i][j-];
}
printf("%d\n", d[len1][len2]);
}
return ;
}