UVALive - 7544 Banking II(dp)

35 篇文章 0 订阅
11 篇文章 0 订阅

点击打开题目链接

A month ago at the South Pacific Divisional Contest, each of you (or one of your teammates) solved a problem about authenticating users. To refresh your memory:
The Actuarial Commerce Merchant bank has a scheme where, when you login, you are provided
with a “pattern word”, containing only upper and lower case letters. You must use this pattern word
to extract and sum digits from your PIN as follows.
Letters in the pattern word are to be interpreted as numbers, with a (or A) = 1, b (or B) = 2, . . .
, z (or Z) = 26. A lower case letter specifies a count of digits to extract from the PIN while an upper
case letter specifies a count of digits to be skipped. The letters in the pattern word are processed from
left to right resulting in a sequence of extracted digits, which are added together to yield a number.
You then enter that number into a field on the web page form to authenticate yourself. For example, if
your PIN was 1093373, and the pattern provided to you was aBcA you would extract one digit (namely

  1. skip two digits (09), extract 3 digits (337) and then skip 1 digit (3), before totalling the extracted
    digits (1337) and entering 14 into the field on the web page form.
    The bank allows you to have a PIN containing up to 256 digits and they provide a pattern word in
    which the letters, when interpreted as numbers, sum to the length of the PIN.
    Between the Divisional contest and now, something terrible has happened: someone has hacked
    into the bank’s database and deleted all of the capital letters from every pattern word! In a panic, the
    bank has called you and requires your help again! For a given partial PIN, they would like to know
    the maximum value that could have been output from the algorithm mentioned above over all possible
    placements of capital letters such that the length of the pattern (when interpreted as numbers) matches
    the length of the PIN. The order of the lowercase letters may not be changed.
    ##Input
    The input file contains several test cases, each of them as described below.
    The first line of input will contain an n-digit PIN (6 ≤ n ≤ 256). The second line will contain an
    m-digit pattern word containing only lower case letters (0 ≤ m ≤ n).
    ##Output
    For each test case, output the maximum possible sum of extracted digits from the PIN. You may assume
    that at least one valid pattern exists (the bank had removed all of the bad cases after your help at
    Divisionals).
    ##Sample Input

092384907653
bc
092384907653
bb

##Sample Output

32
26

##题目大意
给出一个数字串。给出一个小写字母串代表子串长度,小写字母串对应1-26数字。
要求依次在数字串中找到小写字母串对应的数字长度的子串,将子串拼起来使得所有数字相加最大。例如数字串1234,字母串ab,a的子串为2,b的子串为34时总串的和最大为2+3+4=9.
##思路
dp[i][j]代表当子串长度为(s[i]-‘a’+1)时在区间[0, j+(s[i]-‘a’+1)]内子串和的最大值。
然后遍历长度数组,不断更新dp[i][j]的值。
以例1为例。
0 9 2 3 8 4 9 0 7 6 5 3
ab

子串长度\数组01234567891011
a9115->111112139->137->131311->138->1313
b111413+9=2115+11=2621+11=3213+11=26->3216+12=28->3213+13=26->3218+13=31->3214+13=27->323232

##代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn = 276;
int arr[maxn], len[maxn], dp[maxn][maxn];

int main() {
    //ios::sync_with_stdio(false);
    string str, s;
    while(cin >> str) {
        for(int i = 0; i < str.length(); i++) 
            arr[i] = str[i] - '0';
        getchar();
        getline(cin, s);
        int l = s.size();
        for(int i = 0; i < l; i++) 
            len[i] = s[i] - 'a' + 1;
        for(int i = 0; i < l; i++) { 
            for(int j = 0; j < str.length(); j++) {
                int tmp = 0;
                for(int u = 0; u < len[i] && j + u < str.length(); u++) 
                    tmp+= arr[j + u];
                dp[i][j] = tmp;
                if(j > 0) {
                    if(i > 0 && j - len[i - 1] >= 0) 
                        dp[i][j] += dp[i-1][j-len[i-1]];
                    dp[i][j] = max(dp[i][j], dp[i][j-1]);
                }
            }
        }
        cout << dp[l - 1][str.length() - 1] << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值