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 
#include 
#include 
#include 
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;
}


你可能感兴趣的:(UVA,dp,acm-icpc,uva)