uva 11258 String Partition(DP)

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 <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;

#define ll long long int

const int maxn = 210;
const ll M = 2147483647;
ll dp[maxn];
string str;

void initial(){
	for(int i = 0;i < maxn;i++){
		dp[i] = 0;
	}
	str.clear();
}

void computing(){
	cin >> str;
	str = "0" + str;
	int len = str.length();
	//cout << str << endl;
	for(int i = 1;i < len;i++){
		dp[i] = dp[i-1];
		for(int j = i-1;j >= 0;j--){
			if(str[j+1] != '0'){
				string tem = str.substr(j+1 , i-j);
				stringstream ss;
				ss << tem;
				ll t;
				ss >> t;
				if(t > M){
					break;
				}
				dp[i] = max(dp[j]+t , dp[i]);
			}
		}
	}
	cout << dp[len-1] << endl;
}

int main(){
	//cout << M << endl;
	int N;
	cin >> N;
	while(N--){
		initial();
		computing();
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值