UVA11258 String PartitionDP
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11258 String PartitionDP相关的知识,希望对你有一定的参考价值。
John was absurdly busy for preparing a programming contest recently. He wanted to create a ridiculously easy problem for the contest. His problem was not only easy, but also boring: Given a list of non-negative integers, what is the sum of them?
However, he made a very typical mistake when he wrote a program to generate the input data for his problem. He forgot to print out spaces to separate the list of integers. John quickly realized his mistake after looking at the generated input file because each line is simply a string of digits instead of a list of integers.
He then got a better idea to make his problem a little more interesting: There are many ways to split a string of digits into a list of non-zero-leading (0 itself is allowed) 32-bit signed integers. What is the maximum sum of the resultant integers if the string is split appropriately?
Input
The input begins with an integer N (≤ 500) which indicates the number of test cases followed. Each of the following test cases consists of a string of at most 200 digits.
Output
For each input, print out required answer in a single line.
Sample Input
6
1234554321
5432112345
000
121212121212
2147483648
11111111111111111111111111111111111111111111111111111
Sample Output
1234554321
543211239
0
2121212124
214748372
5555555666
问题链接:UVA11258 String Partition
问题简述:(略)
问题分析:文本处理问题,用动态规划来解决,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA11258 String Partition */
#include <bits/stdc++.h>
using namespace std;
const long long LIMIT = 2147483647LL;
const int L = 200 + 1;
long long dp[L + 1];
char s[L];
int main()
{
int t;
scanf("%d", &t);
while (t--) {
scanf("%s", s);
int len = strlen(s);
memset(dp, 0, sizeof dp);
for (int i = 0; i < len; i++)
if (s[i] == '0')
dp[i + 1] = max(dp[i + 1], dp[i]);
else {
long long t = 0;
for (int j = i; j < len; j++) {
t = t * 10 + s[j] - '0';
if (t > LIMIT) break;
dp[j + 1] = max(dp[j + 1], dp[i] + t);
}
}
printf("%lld\\n", dp[len]);
}
return 0;
}
以上是关于UVA11258 String PartitionDP的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode11.Array and String —Array Partition I 数组分区
DNA Consensus String,( UVa, 1368 )
UVA - 1368 DNA Consensus String