UVALive 7544 Banking II (DP)

原题:

A month ago at the South Pacifi 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 specifis a count of digits to extract from the PIN while an upper
case letter specifis 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 fild 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 fild 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 fie contains several test cases, each of them as described below.
The fist 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


题意:

      根据小写字母的字典序确定所选择连续数字个数,由自给定字母串序列顺序得出最大数字和


思路:
       直接暴搜一遍超时,确定dp的方法。dp[i][j]表示第i字母以第j个数字时的数字和。dp[i][j]=max(dp[i-1][k] ||k>=s[i-1]&&k<=j-cnt[i])+sum[j]-sum[j-cnt[i]]。 s[i]为字母串所选数字个数的前缀和,cnt[i]为当前字母所选数字个数,sum[i]为数字串的前缀和。



注意m可能等于0并且字母串所选个数可能大于数字串数字个数。


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300;

char str1[maxn], str2[maxn];
int n, m, sum[maxn], s[maxn], cnt[maxn], dp[maxn][maxn], ans;

int main() {
    while (gets(str1 + 1) != NULL) {
        gets(str2 + 1);
        n = strlen(str1 + 1), m = strlen(str2 + 1);
        if (m == 0) {
            printf("0\n"); continue;
        }
        memset(sum, 0, sizeof(sum));
        for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + str1[i] - '0';
        for (int i = 1; i <= m; i++) cnt[i] = str2[i] - 'a' + 1;
        memset(s, 0, sizeof(s));
        for (int i = 1; i <= m; i++) s[i] = s[i - 1] + cnt[i];
        memset(dp, 0, sizeof(dp));
        ans = 0;
        for (int i = 1; i <= m; i++) {
            for (int j = s[i]; j <= n; j++) {
                int temp = 0;
                for (int k = s[i - 1]; k <= j - cnt[i]; k++) {
                    temp = max(temp, dp[i-1][k]);
                }
                dp[i][j] = temp + sum[j] - sum[j - cnt[i]];
            }
        }
        if (s[m] >= n) {
            printf("%d\n", sum[n]); continue;
        }
        for (int i = 1; i <= n; i++) ans = max(ans, dp[m][i]);
        printf("%d\n", ans);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值