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)) { ...
随机推荐
- 译 PrestaShop开发者指南 第四篇 深入PrestaShop核心开发
## 访问数据库 ### 数据库结构 PrestaShop的数据库表默认带有ps_的前缀,前缀在安装时可以自定义. 所有表名都是小写,以下划线分割.当一个表表示要在两个实体间建立连接时,表名中两个实体 ...
- 使用jQuery库改造ajax
html页 ---------------------------------------------------------------------------------------------- ...
- 在Hadoop平台跑python脚本
1.开发IDE,我使用的是PyCharm. 2.运行原理 使用python写MapReduce的“诀窍”是利用Hadoop流的API,通过STDIN(标准输入).STDOUT(标准输出)在 ...
- javascript宿主对象之window.frames
window.frames属性是当前页面所有框架的集合.要注意的事,这里并没有frame和iframe做出区分.而且,无论页面存不存在框架,window.frames属性总是存在的,并总是指向wind ...
- Web打印控件
Lodop是什么? 有人说她是Web打印控件,因为她能打印.在浏览器中以插件的形式出现,用简单一行语句就把整个网页打印出来: 有人说她是打印编程接口,因为她介于浏览器和打印设备之间,是个通道和桥梁,几 ...
- redis实现主从复制-单机测试
一.redis实现主从复制-单机测试1.安装redis tar -zxvf redis-2.8.4.tar.gzcd redis-2.8.4make && make install2. ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q25-Q27)
Question25You develop a new publishing page layout named MyPage.aspx for a SharePoint site.You creat ...
- Android平台二维码之生成,扫描 & 识别
1.二维码的前世今生 “二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利 ...
- Bootstrap的优先级、选择器、伪类
概述:Bootstrap的CSS组件的核心就是选择器的定义以及在各自优先级上的处理.由于大部分的选择器都非常的常见就一笔带过了,这里重点介绍一下Bootstrap用到的知识点. 一.优先级 之前我们使 ...
- 如何成功发布一个MSMQ的Windows服务
因为MSMQ的使用需要不断的查看队列是否有新消息,所以一般是结合Windows的服务,当然也可以用一个不关闭的Winform程序,不过前者更好一些,不怕被人误关. 完成MSMQ的WindowsServ ...