uav 11258 String Partition (DP)
2016-01-04 09:49
501 查看
Problem F - String Partition
Time limit: 3.000 seconds
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型的数字,而且使这些数字之和最大。
思路:dp[i]表示前i个字符划分时的和的最大值。
dp[i] = max(dp[i], dp[i-j]+num),num为从后往前划分为j长度的数字。
Time limit: 3.000 seconds
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型的数字,而且使这些数字之和最大。
思路:dp[i]表示前i个字符划分时的和的最大值。
dp[i] = max(dp[i], dp[i-j]+num),num为从后往前划分为j长度的数字。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <climits> #define LL long long using namespace std; const LL inf=INT_MAX; const int maxn=210; LL dp[maxn]; string str; void initial() { memset(dp,0,sizeof(dp)); } void input() { cin>>str; } int get_num(char ch) { return ch-'0'; } void solve() { int len=str.size(); dp[1]=get_num(str[0]); for(int i=2;i<=len;i++) { LL tmp=1,now=0; for(int k=i-1,j=1;k>=0;k--,j++) { now=get_num(str[k])*tmp+now; if(now>inf) break; dp[i]=max(dp[i],dp[i-j]+now); tmp*=10; } } printf("%lld\n",dp[len]); } int main() { int T; scanf("%d",&T); while(T--) { initial(); input(); solve(); } return 0; }
相关文章推荐
- Firefox浏览器 页面滑动卡帧问题
- python性能优化
- 学习javascript面向对象 理解javascript对象
- Java中的String,StringBuffer,StringBuilder的区别
- curl设置cookie
- 《Java编程思想》学习笔记11——Java I/O
- 异步Servlet编程 | Servlet3.0新特性
- 用Redis存储Tomcat集群的Session
- 配置ab来为Nginx服务器做压力测试的方法
- web前端的国际化 i18n
- 最新隐马尔可夫模型HMM详解
- CRS-2728: A resource type with the name 'ora.daemon.type' is already registered
- android swipeRefreshLayout 下拉刷新 geogle官方组件
- jquery禁用右键功能和f5刷新
- 给用户权限
- APP上架苹果商店必看 如何通过苹果App Store审核
- 服务器以流的形式返回一个本地文件给请求方
- 如何解决MathType特殊字符在InDesign中的这些问题
- 《Java编程思想》学习笔记10——文件和目录常用操作
- Oracle列转行函数listagg和vmsys.vm_concat