您的位置:首页 > 其它

ZOJ 1642 Match for Bonus

2016-12-22 16:20 417 查看
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
, x, l, r, g

;
char s
, a
, b
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: