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长度的数字。

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

uav 11258 String Partition (DP)的更多相关文章

  1. leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

    1048. Longest String Chain https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a ...

  2. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

  3. HDU4681 String(dp)

    题目链接. #include <iostream> #include <cstdio> #include <cstring> #include <cstdli ...

  4. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  5. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  6. hdu5707-Combine String(DP)

    Problem Description Given three strings a, b and c , your mission is to check whether c is the combi ...

  7. 10.Regular Expression Matching (String; Back-Track,DP)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. HDU 4055 Number String (计数DP)

    题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果第i字符是‘I’表示排列中的第i-1个数是小于第i个数的. 如果是‘D’,则反之. 析:dp[i][j] 表示前 i ...

  9. hdu 4055 Number String (基础dp)

    Number String Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Android开源项目pulltorefresh分析与简单使用

    在Android开发中有时我们须要訪问网络实时刷新数据.比方QQ好友在线状态最新信息,QQ空间须要显示很多其它的好友动态信息,EOE论坛client显示很多其它的文章帖子信息等.android-pul ...

  2. [leetcode]3 Sum closest

    问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  3. shell 脚本之if、for、while语句

    (1)if语句 root@ubuntu:/mnt/shared/shellbox/shellif# cat shellif.sh #!/bin/bash #推断字符串 if [ "$1&qu ...

  4. PHP实现插入排序算法

    插入排序(Insertion Sort),是一种较稳定.简单直观的排序算法.插入排序的工作原理,是通过构建有序序列,对于未排序的数据,在有序序列中从后向前扫描,找到合适的位置并将其插入.插入排序,在最 ...

  5. Knockout应用开发指南 第七章:Mapping插件

    原文:Knockout应用开发指南 第七章:Mapping插件 Mapping插件 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些 ...

  6. C#语言基础原理及优缺点

    一.原理: C#是专门为.net程序框架而创造的语言. .net框架有ms的.netFramework:Mono的.NetFramework(也是符合.net IL语言,CTS规范,CLS规范, CL ...

  7. 《JavaScript设计模式与开发实践》读书笔记之观察者模式

    1.观察者模式 观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. JavaScript中通常采用事件模型替代传统的观察者模式 1.1 逐步实现观 ...

  8. 意外地解决了一个WPF布局问题

    原文:意外地解决了一个WPF布局问题 今天做了一个小测试,意外地将之前的一个困扰解决了,原问题见<WPF疑难杂症会诊>中的“怎么才能禁止内容撑大容器?” 以前我是在外侧嵌套Canvas容器 ...

  9. JDBC连接数据库和释放连接

    用久了hibernate现在对于JDBC是怎么实现数据库的连接和释放,所以特地总结下关于JDBC的知识,目的是用于提醒自己很多Java的基础知识需要健全. package com.ssh.action ...

  10. ASP.NET——验证码的制作

            我们在登陆站点,发表博客或者提交评论的时候,常常会遇到填写验证码这一项,当时感觉挺奇妙的样子,最终在牛腩新闻公布系统里接触到了,在这里小小的总结下.         用到的东东有三个: ...