24h購物| | PChome| 登入
2012-04-01 13:02:38| 人氣1,321| 回應0 | 上一篇 | 下一篇

[UVA][DP] 11258 - String Partition

推薦 0 收藏 0 轉貼0 訂閱站台

Problem F - String Partition


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

動態規劃

#include <stdio.h>
#include <string.h>
const long long limit = 2147483647LL;
long long max(long long x, long long y) {
    return x > y ? x : y;
}
int main() {
    int t, i, j;
    char str[201];
    scanf("%d", &t);
    while(t--) {
        scanf("%s", str);
        int slen = strlen(str), i;
        long long DP[202] = {}, tmp = 0;
        for(i = 0; i < slen; i++) {
            if(str[i] == '0') {
                DP[i+1] = max(DP[i+1], DP[i]);
            } else {
                tmp = 0;
                for(j = i; j < slen; j++) {
                    tmp = tmp*10 + str[j]-'0';
                    if(tmp > limit)
                        break;
                    DP[j+1] = max(DP[j+1], DP[i]+tmp);
                }
            }
        }
        printf("%lld\n", DP[slen]);
    }
    return 0;
}

台長: Morris
人氣(1,321) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Map] 10420 - List of Conquests
此分類上一篇:[UVA] 12004 - Bubble Sort

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文