uva 11258 - 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

 

题意:将给出的字符串直接拆成若干个int大小内的数,求拆开后获得的数的最大和。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
int T,len;
char a[550];
long long f[550];
void ready()
{
	//ios::sync_with_stdio(false),cin.tie(0);
	cin>>T;
	return ;
}
void read_in()
{
	scanf("%s",a+1);
	len=strlen(a+1);
	memset(f,0,sizeof(f));

	return ;
}
long long get_sum(int l,int r)
{
	long long re=0;
	for(int i=l;i<=r;i++)
		re=re*10+int(a[i])-'0';
	return re;
}
void do_it()
{
	for(int i=1;i<=len;i++)
	{
		for(int j=1;j<=i;j++)
		{
			long long sum=get_sum(j,i);
			if(sum>=0 && sum<=2147483647)
			{
				 f[i]=max(f[i],f[j-1]+sum);		 
			}
			 
		}
	}
	cout<<f[len]<<endl;
}
int main()
{
	ready();
	while(T--)
	{
		read_in();
		do_it();
	}
	return 0;
}

 

全部评论

相关推荐

accaacc:2到4k,不是2k到4k,所以年薪是30块
点赞 评论 收藏
分享
11-04 14:10
东南大学 Java
_可乐多加冰_:去市公司包卖卡的
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务