quicksum

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.

Examples

0)

"99999"

45

Returns: 4

In this case, the only way to achieve 45 is to add 9+9+9+9+9. This requires 4 additions.

1)

"0123456789"                        01+2+3+4+5+6+7+8+9

45

Returns: 8

一般的DFS解法:

(下附代码中并没有写无解时输出-1的部分,懒得补了233)

 /*Silver_N quicksum*/
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int n[];//每个数字
int re[][];
int ct=;//总字符数
int mini=;//最小解
int m;//需求数
int ansum=;
int ssum(int s,int t){//截取字符串的函数,通过记忆化避免重复计算
int i,j;
if(re[s][t])return re[s][t];
//else begin
int x=;
for(i=s;i<=t;i++){
x=x*+n[i];
}
re[s][t]=x;
return x;
//end
}
void read1(){//数据读入
bool flag=;
char c;
while(scanf("%c",&c)){
if(c=='"')
if(flag==)break;
else flag=;
if(c>='' && c<=''){
n[++ct]=c-'';
}
}
return;
}
void dfs(int cpls,int pos){//cpls-已用加号数量 pos-在第pos个数后插入
// printf("test \n");
if(cpls>mini)return;
int i,j;
int start=pos+;
for(i=pos+;i<=ct;i++){
// printf("test message: s:%d t:%d total:%d \n",start,i,ct);
// printf(" sum:%d + %d\n",ansum,ssum(start,i));
ansum+=ssum(start,i);
if(ansum>m){
ansum-=ssum(start,i);
break;}
if(ansum==m && i==ct){//只有i==ct时才算解
mini=min(mini,cpls);
}
else
dfs(cpls+,i);
ansum-=ssum(start,i);
}
return;
}
int main(){
read1();
cin>>m;
dfs(,);
cout<<mini;
return ; }

贪心式DFS:

保证总和不超过所求值m的情况下,每段截取得尽可能大,如此找到的第一个解就是最优解。

最差情况下基本等于暴搜,但平均看来效率还是比较高的

 /*Silver_N    quicksum*/
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int n[];//每个数字
int re[][];
int ct=;//总字符数
int mini=;//最小解
int m;//需求数
int ansum=;
int flag=;
int ssum(int s,int t){//截取
int i,j;
if(re[s][t])return re[s][t];
//else begin
int x=;
for(i=s;i<=t;i++){
x=x*+n[i];
}
re[s][t]=x;
return x;
//end
}
void read1(){//读入
bool flag=;
char c;
while(scanf("%c",&c)){
if(c=='"')
if(flag==)break;
else flag=;
if(c>='' && c<=''){
n[++ct]=c-'';
}
}
return;
}
void dfs(int cpls,int pos){//pos-在第pos个数后插入
// printf("test \n");
if(flag==)return;
if(cpls>mini)return;
int i,j;
int start=pos+;
for(i=ct;i>=pos+;i--){//从后往前枚举位置,优先截取较长的串
// printf("test message: s:%d t:%d total:%d \n",start,i,ct);
// printf(" sum:%d + %d\n",ansum,ssum(start,i));
ansum+=ssum(start,i);
if(ansum>m){
ansum-=ssum(start,i);
continue;}
if(ansum==m && i==ct){
mini=min(mini,cpls);
flag=;//找到解的标志
}
else
dfs(cpls+,i);
if(flag==)return;//找到一解直接退出
ansum-=ssum(start,i);
}
return;
}
int main(){
read1();
cin>>m;
dfs(,);
if(mini==)cout<<"-1";
else cout<<mini;
return ; }

Quicksum -SilverN的更多相关文章

  1. [字符哈希] POJ 3094 Quicksum

    Quicksum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16488   Accepted: 11453 Descri ...

  2. NOIP2010提高组乌龟棋 -SilverN

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  3. NOIP2010提高组 关押罪犯 -SilverN

    (洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...

  4. NOIP2008提高组(前三题) -SilverN

    此处为前三题,第四题将单独发布 火柴棒等式 题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0 ...

  5. NOIP2008普及组 题解 -SilverN

    T1 ISBN号码 题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符, 其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符( ...

  6. ACM——Quicksum

    Quicksum 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:615            测试通过:256 描述 A chec ...

  7. Quicksum

    Quicksum Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  8. POJ3094 Quicksum

    POJ3094 Quicksum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18517   Accepted: 1271 ...

  9. TJU Problem 2520 Quicksum

    注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time L ...

随机推荐

  1. csharp:百度翻译

    参考:http://api.fanyi.baidu.com/api/trans/product/index http://developer.baidu.com/wiki/index.php?titl ...

  2. sql2008 查询字段所属表

    select a.name as 表名, g.*from sysobjects as a left join syscolumns as b on a.id=b.id left JOIN sys.ex ...

  3. redis3.0 集群实战1 -- 安装和配置

    本文主要是在centos7上安装和配置redis集群实战 参考: http://hot66hot.iteye.com/blog/2050676 集群教程: http://redisdoc.com/to ...

  4. uploadify API

    apifunctionjavascriptflashsecurity服务器 属性: uploader : uploadify.swf 文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击 ...

  5. HTML 5 中的标准属性

    HTML 全局属性 HTML 属性赋予元素意义和语境. 下面的全局属性可用于任何 HTML 元素. (5)= HTML5 中添加的属性. 属性 描述 accesskey 规定激活元素的快捷键. cla ...

  6. CRM2013版本 IOS APP 说明(IPhone、IPad)

    CRM2013版本 IOS APP 说明(IPhone.IPad) IPhone版本 首页 CRM APP在登录时输入账号信息,可以进行首面.其首页显示内容可以在CRM后台设置. 系统默认显示:Pho ...

  7. Sharepoint 2013 关于"SPChange"简介

    在SharePoint中,我们经常会需要获取那些改变的项目,其实api为我们提供了SPChange对象,下面,我们通过列表简单介绍下这一对象. 1.创建一个测试列表,名字叫做“SPChangeItem ...

  8. Android WelcomeActivity 启动画更换网络图片

    1.运行效果  第一张是本地的启动图,第二张是网络启动图       2.用到的第三方jar包   Android-Universal-Image-Loader-master 不熟的请看  Andro ...

  9. iOS菜单滚动联动内容区域功能实现

    平时开发APP中关于此功能还是比较经常碰到,本实例借用三个开源的插件,并对其中一个进行修改调整实现出想要的效果:本文重点介绍修改的内容跟三个插件的运用,这三个插件还可以各自扩展到其它项目的运用: 效果 ...

  10. 【读书笔记】iOS-NSNumber

    NSArray和NSDictionary只能存储对象,而不能直接存储任何基本类型的数据,如int,float或struct.但是你可以用对象来封装基本数值.例如,将int型数据封装到一个对象中,然后就 ...