Quicksum-S.B.S.
quicksum
Queation:
Given a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs are inserted, evaluate the sum as usual. For example, consider the string "12" (quotes for clarity). With zero additions, we can achieve the number 12. If we insert one plus sign into the string, we get "1+2", which evaluates to 3. So, in that case, given "12", a minimum of 1 addition is required to get the number 3. As another example, consider "303" and a target sum of 6. The best strategy is not "3+0+3", but "3+03". You can do this because leading zeros do not change the result.
Write a class QuickSums that contains the method minSums, which takes a String numbers and an int sum. The method should calculate and return the minimum number of additions required to create an expression from numbers that evaluates to sum. If this is impossible, return -1.
example:
"382834"
100
Returns: 2
There are 3 ways to get 100. They are 38+28+34, 3+8+2+83+4 and 3+82+8+3+4. The minimum required is 2.
Constraints
- numbers will contain between 1 and 10 characters, inclusive.
- Each character in numbers will be a digit.
- sum will be between 0 and 100, inclusive.
- the string will be shorter than 100 bit.
---------------------------------------------我是分割线--------------------------------------------------------------
本题有多种方法,例如“记忆化搜索+剪枝”,但我用的是DP。
由于数据太弱(加号数小于10,和不大于100……)所以开个三维数组dp[i][j][k]。
i、j表示字符串从i开始到j表示的数;
k表示此时和为k;
数组内存放所需加号数。
不多说,上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int cut(string,int,int);
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
long long chang=;
int dp[][][];
int main()
{
string s;long long sum;long long ans=;
cin>>s;
chang=s.length();
cin>>sum;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++)
dp[i][j][k]=;
// cout<<dp[0][chang-1][sum]<<endl;
for(int i=;i<chang;i++)
for(int j=;i+j<chang;j++)
{
long long num=cut(s,i,i+j);
if(num<=sum) dp[i][i+j][num]=;
}
// cout<<dp[0][chang-1][sum]<<endl;
for(int i=;i<chang;i++) //数长
for(int head=;head+i<chang;head++) //始位
for(int j=;j<=sum;j++) //和
for(int k=head;k<head+i;k++) //加号位
for(int ss=;j-ss>;ss++) //中间和
dp[head][head+i][j]=min((dp[head][k][j-ss]+dp[k+][head+i][ss])+,dp[head][head+i][j]);
ans=dp[][chang-][sum];
if(ans==) ans=-;
cout<<ans;
return ;
}
int cut(string s,int a,int b)
{
long long n=;
for(int i=a;i<=b;i++)
n=n*+(s[i]-'');
return n;
}
Quicksum-S.B.S.的更多相关文章
- [字符哈希] POJ 3094 Quicksum
Quicksum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16488 Accepted: 11453 Descri ...
- Quicksum -SilverN
quicksum Given a string of digits, find the minimum number of additions required for the string to e ...
- ACM——Quicksum
Quicksum 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:615 测试通过:256 描述 A chec ...
- Quicksum
Quicksum Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Subm ...
- POJ3094 Quicksum
POJ3094 Quicksum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18517 Accepted: 1271 ...
- TJU Problem 2520 Quicksum
注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520. Quicksum Time L ...
- H - Quicksum(1.5.3)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
- HDU.2734 Quicksum
Quicksum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- POJ 3094 Quicksum 难度:0
http://poj.org/problem?id=3094 #include<iostream> #include <string> using namespace std; ...
- poj 3094 Quicksum
#include <stdio.h> #include <string.h> ]; int main() { ; int i,len; while(gets(word)) { ...
随机推荐
- T-SQL的回车和换行符(SQL)
T-SQL的回车和换行符(SQL) sql server中的回车换行字符是 char(13)+char(10) 回车:char(13) 换行:char(10) 实例1: DECLARE @c NVA ...
- 以Self Host的方式来寄宿Web API
Common类及实体定义.Web API的定义请参见我的上一篇文章:以Web Host的方式来寄宿Web API. 一.以Self Host寄宿需要新建一个Console控制台项目(SelfHost) ...
- weblogic 12c下jxls导出excel报错Could not initialize class org.apache.poi.xssf.usermodel.XSSFVMLDrawing
周一,开发反馈weblogic 12c下jxls导出excel报错,公司环境和UAT环境均报错,看日志如下: 2016-06-08 09:16:55,825 ERROR org.jxls.util.T ...
- OS X 在Cisco无线环境下丢包分析 part 1
补发一篇博客,之前遇到的没有写成博文的一个情况.我擦,那一阵儿真是被无线搞疯了. 现象:苹果OS X用户连入WiFi之后莫名丢包,而且有规律的丢,丢个5s恢复正常,再过会儿再丢5s左右. 就如同这样 ...
- 自制javascript游戏-点燃火绳
自制javascript游戏-点燃火绳 这是一款多关卡的游戏,目录有21个地图,游戏采纯原生 js库JY编写,所以编写得很简单迅速,这款游戏的思路来源于,一个人撸管太多,手会不会连鼠标也拿不稳,为了验 ...
- Oracle之自动收集统计信息
一.Oracle 11g 在Oracle的11g版本中提供了统计数据自动收集的功能.在部署安装11g Oracle软件过程中,其中有一个步骤便是提示是否启动这个功能(默认是启用这个功能). 在这里介绍 ...
- SharePoint 2013 中自定义WCF服务
在使用SharePoint2013的时候,如果其他客户端 API 的组合不足,可以通过自定义 Web 服务扩展 SharePoint.默认情况下,SharePoint 2013 不仅支持创建自定义 A ...
- NDK、SDK以及JNI的关系
最近由于项目的需要,使用到了Android的NDK技术,对项目核心算法跨平台的移植.简答而言,就是使用C对原来的算法进行了改进,并集成到原 来的app项目里. 从前的项目一直没有使用NDK进行开发的机 ...
- CSS 子选择器(六)
一.子选择器 子选择器中前后部分之间用一个大于号隔开,前后两部分选择符在结构上属于父子关系. 子选择器是根据左侧选择符指定的父元素,然后在该父元素下寻找匹配右侧选择符的子元素. 二.简单例子 < ...
- 【转】Visual Studio 非常实用的调试技巧
下面有从浅入深的6个问题,您可以尝试回答一下 一个如下的语句for (int i = 0; i < 10; i++){if (i == 5)j = 5;},什么都写在一行,你怎么在j=5前面插入 ...