ZOJ 1642 Match for Bonus
原创
©著作权归作者所有:来自51CTO博客作者qq636b7aec0b3f1的原创作品,请联系作者获取转载授权,否则将追究法律责任
Roy played a game with his roommates the other day.
His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common character in the 2 strings, the bonus value of that character would be added to Roy's score. However at the mean time, the 2 characters and those before them in the strings would be erased.
Roy now needs your help, because he wants to know the maximum score he can get.
Input
There are several test cases.
For each test case, the first line contains an integer N.
The following N lines describe a list of N characters and their bonus values.
Then the following 2 lines give the 2 strings.
Output
For each test case, output in one line the best score Roy can get.
Sample Input
3
a 1
b 1
c 1
abc
bca
3
a 1
b 10
c 100
abc
cab
Sample Output
2
100
类似LCS,不过因为多了权值,所以条件稍有修改
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-5;
const int mod = 1e9 + 7;
const int N = 2e3 + 10;
const int INF = 0x7FFFFFFF;
int n, f[N], x, l, r, g[N][N];
char s[N], a[N], b[N];
int main()
{
while (~scanf("%d", &n))
{
rep(i, 1, n)
{
scanf("%s%d", s, &x);
f[s[0]] = x;
}
scanf("%s%s", a + 1, b + 1);
l = strlen(a + 1); r = strlen(b + 1);
rep(i, 1, l) rep(j, 1, r)
{
g[i][j] = max(g[i][j - 1], g[i - 1][j]);
if (a[i] == b[j]) g[i][j] = max(g[i][j], g[i - 1][j - 1] + f[a[i]]);
}
printf("%d\n", g[l][r]);
}
return 0;
}