UVA 11258 String Partition(线性DP)

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-bitsignedintegers. What is the maximum sum of the resultant integers if the string is split appropriately?InputThe input begins with an integerN(≤500) which indicates the number of test cases followed. Each of the following test cases consists of a string of at most 200 digits.OutputFor each input, print out required answer in a single line.Sample input

612345543215432112345000121212121212214748364811111111111111111111111111111111111111111111111111111

Sample output

1234554321543211239021212121242147483725555555666

Problem setter: ChoSource: Tsinghua-HKUST Programming Contest 2007

题意就是说有个数字串,让你拆分成不超过INT_MAX的数。

求最大和。dp[i]:前i个数的最大和。dp[i]=max(dp[k]+sum(k~i)).

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<limits.h>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=220;LL dp[maxn];char str[maxn];int main(){int t;scanf("%d",&t);while(t–){scanf("%s",str+1);int len=strlen(str+1);CLEAR(dp,0);REPF(i,1,len){for(int l=15;l>=1;l–){if(i-l<0) continue;LL num=0;for(int j=i-l+1;j<=i;j++)num=num*10+str[j]-'0';if(num<0||num>INT_MAX) continue;dp[i]=max(dp[i],dp[i-l]+(LL)num);}}printf("%lld\n",dp[len]);}return 0;}

,趁着有脾气装潇洒,有本钱耍个性,

UVA 11258 String Partition(线性DP)

相关文章:

你感兴趣的文章:

标签云: