设为首页 加入收藏

TOP

UVA 11258 String Partition(线性DP)
2015-07-20 17:13:08 来源: 作者: 【 】 浏览:2
Tags: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 signedintegers. 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


Problem setter: Cho
Source: Tsinghua-HKUST Programming Contest 2007

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

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

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
           #include
           
             #include
            
              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
             
              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; } 
             
            
           
         
        
       
      
     
    
   
  

 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇leetcode Minimum Depth of Binar.. 下一篇POJ 题目3164 Command Network(..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·有没有哪些高效的c++ (2025-12-27 08:20:57)
·Socket 编程时 Accep (2025-12-27 08:20:54)
·计算机网络知识点总 (2025-12-27 08:20:52)
·一篇说人话的文章, (2025-12-27 07:50:09)
·Python Web框架哪家 (2025-12-27 07:50:06)