UVA 题目11258 - String Partition(DP)
2015-12-03 22:20
441 查看
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
题目大意:一个字符串切成若干串,每个串一个整数,问切割获得的最大和
ac代码
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
题目大意:一个字符串切成若干串,每个串一个整数,问切割获得的最大和
ac代码
#include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #include<limits.h> #define LL long long using namespace std; char str[220]; LL dp[220]; int n; void solve() { int i,j,k; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { for(j=1;j<=i&&j<=10;j++) { LL sum=0; for(k=1;k<=j;k++) { sum=sum*10+(str[i-j+k]-'0'); } if(sum<=INT_MAX) dp[i]=max(dp[i],dp[i-j]+sum); } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s",str+1); n=strlen(str+1); solve(); printf("%lld\n",dp ); } }
相关文章推荐
- SQL入门笔记
- 通过socket数据报传输消息
- WebKit之chrome的构建系统工具gyp(类比于qmake)
- svn报错:Cannot negotiate authentication mechanism
- 泛型的基本介绍和使用
- OC基础(27)
- if 语句练习 身高体重问题
- 理解长短期记忆网络(LSTM NetWorks)
- 黑马程序员——Java笔记——String类的面试题
- HDU 1016Presentation Error
- 国外程序员整理的Java资源大全
- jqueryHTML相关方法
- OC基础(26)
- 原型设计(“留拍”Axure整体操作过程)
- CoffeeScript ------ 问号
- ZABBIX安装配置过程
- Quora上关于P, NP, NP-complete, and NP-hard问题的解答
- R语言数据处理(一)
- Mac使用技巧
- windows10安装ubuntu15.10后只有ubuntu启动项