ZOJ 1642 Match for Bonus(dp)

Match for Bonus

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

题意:有两个字符串,每个串由n个字符组成,每个字符有一个价值,Roy每次指定串2中的一个字符,他的得分增加的值为这个字符的价值,然后把两个串中这个字符前面的那部分(包括这个字符)删掉,重复进行这样的操作,求Roy最多能得多少分。

分析:听队友说,这题和LCS差不多,比赛时我在做另外一道题,当时就没做。比赛完才做的这道题,确实和LCS太像了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef long long LL;

map<char, int> mp;
string s1, s2;
const int N = 2010;
int dp[N][N];
int fun()
{
    memset(dp, 0, sizeof(dp));
    int len1 = s1.length(), len2 = s2.length();
    for(int i = 1; i <= len1; i++) {
        for(int j = 1; j <= len2; j++) {
            if(s1[i-1] == s2[j-1])
                dp[i][j] = dp[i-1][j-1] + mp[s1[i-1]];
            else
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    return dp[len1][len2];
}
int main()
{
    int n, val;
    char ch;
    while(cin >> n) {
        mp.clear();
        for(int i = 0; i < n; i++) {
            cin >> ch >> val;
            mp[ch] = val;
        }
        cin >> s1 >> s2;
        cout << fun() << endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值