UVa 11258 - String Partition(dp)
2015-08-02 17:17
477 查看
String Partition
Description
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
61234554321
5432112345
000
121212121212
2147483648
11111111111111111111111111111111111111111111111111111
Sample output
1234554321543211239
0
2121212124
214748372
5555555666
题意:将这个字符串分解成若干个,每一个转化成10进制之后不能大于int,问最大和是多少
思路:从后往前推进就行了
[code]#include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=210; char s[maxn]; LL dp[maxn]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",s); memset(dp,0,sizeof(dp)); int len=strlen(s); for(int i=len-1;i>=0;i--) { LL sum=0; for(int j=i;j<len;j++) { sum=sum*10+s[j]-'0'; if(sum>INT_MAX)break; dp[i]=max(dp[i],dp[j+1]+sum); } } cout<<dp[0]<<endl; } return 0; }
相关文章推荐
- 【zoj3524】【Crazy Shopping】
- 图论浅析--最短路之Bellman-Ford
- hihoCoder 1069 最近公共祖先·三
- 图论浅析--最短路之Bellman-Ford
- laravel中的$request对象构造及请求生命周期
- Where is the Marble?
- Leetcode #223 Rectangle Area
- diffork.c
- 一个简单的模板引(han)擎(shu)
- 阿里2013双十一备战中的技术突破
- window删除损坏无法打开的文件
- 多校4-Walk Out
- AVL 树的插入和删除
- 奈氏准则和香农公式
- OGRE-降服巨兽
- 多校4-Walk Out 分类: 比赛 2015-08-02 17:15 21人阅读 评论(0) 收藏
- MySQL查询优化之COUNT()
- linux中使用LVM动态扩容
- iOS 导航连续退出2个页面
- 常见端口表汇总