UVALive

来源:互联网 发布:秋之风腊肠淘宝 编辑:程序博客网 时间:2024/06/08 16:48

点击打开题目链接

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

子串长度\数组 0 1 2 3 4 5 6 7 8 9 10 11 a 9 11 5->11 11 12 13 9->13 7->13 13 11->13 8->13 13 b 11 14 13+9=21 15+11=26 21+11=32 13+11=26->32 16+12=28->32 13+13=26->32 18+13=31->32 14+13=27->32 32 32

代码

#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;}
原创粉丝点击