UVALive - 7544 Banking II 朴素dp、类似于背包的dp
来源:互联网 发布:浙江卫视网络直播源 编辑:程序博客网 时间:2024/05/22 03:28
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 uppercase 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 (namely1) 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 anm-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
Source
UVALive - 7544
My Solution
题意:给出一个数字字符串,然后给出一个由小写字母构成的字符串,每个小写字母x 表示 有且必须选择一段连续的长度为 x - 'a' + 1的数字字符,然后要求这些小写字母按顺序选取数字字符串的子串,求选取的数字的和的最大值
朴素dp、类似于背包的dp
定义dpij 表示当前在考略第i个小写字母,将要选取的数字字符串子串是 [j - ( k[i] - 'a' + 1), j],时已经选中的数字的和的最大值,
每次初始为 dpij = dpi-1j,然后进行转移即可
复杂度 O(n^2)
#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int maxn = 256 + 8;int dp[maxn][maxn], sum[maxn];string s1, s2;int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("c.out", "w", stdout); int T = 1; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int sz1, sz2, i, j, ans = 0; while(getline(cin, s1)){ memset(dp, 0, sizeof dp); memset(sum, 0, sizeof sum); //cin >> s2; getline(cin, s2); sz1 = s1.size(), sz2 = s2.size(); //sum[] = s1[0] - '0'; for(i = 0; i < sz1; i++){ sum[i + 1] = sum[i] + (s1[i] - '0'); } ans = 0; for(i = 0; i < sz2; i++){ for(j = 1; j <= sz1; j++){ dp[i][j] = dp[i][j-1]; if(i != 0) { if(j - (s2[i] - 'a' + 1) >= 0) dp[i][j] = max(dp[i][j], dp[i-1][j - (s2[i] - 'a' + 1)] + sum[j] - sum[j - (s2[i] - 'a' + 1)]); } else{if(j - (s2[i] - 'a' + 1) >= 0) dp[i][j] = max(dp[i][j], sum[j] - sum[j - (s2[i] - 'a' + 1)]);} ans = max(ans, dp[i][j]); } } cout << ans << "\n"; } #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Tnak you!
------from ProLights
- UVALive - 7544 Banking II 朴素dp、类似于背包的dp
- UVALive 7544 Banking II (DP)
- uvalive 4015 树形背包dp
- uvalive 4015 洞穴cave(树的dp/01背包)
- UVALIve 4256 图的dp
- HDU 2296 Ring (AC自动机 + dp[类似于背包])
- lightoj - 1193 - Dice (II) dp / 背包
- HDU 4433 类似于状态压缩的DP
- vijos 1240 朴素的网络游戏 dp
- DP背包
- 背包 dp
- 背包dp
- 背包dp
- DP-背包
- 背包dp
- 背包dp
- 背包dp
- UVALive 4725 Airport 【DP】
- 深度优先遍历DFS 与 广度优先遍历BFS(java实现)
- Tensorflow快速入门3–代码结构及模型例子
- 寒假13:黑白无常、最大体积
- CSS页面渲染优化属性will-change
- 欢迎使用CSDN-markdown编辑器
- UVALive - 7544 Banking II 朴素dp、类似于背包的dp
- WAMP安装配置
- 141. Linked List Cycle
- 【深度学习:CNN】如何高效的通过BP算法来训练CNN
- c++ visitor 设计模式
- [LeetCode]467. Unique Substrings in Wraparound String
- 【UVA297】四象树(BZOJ2664)
- C++利用指针突破private
- android控制台灯