您的位置:首页 > 其它

UVALive - 7544 Banking II(dp)

2017-12-04 17:29 351 查看
点击打开题目链接

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp