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. 【原创】C#通用权限管理-程序安全检查,这些你一定要考虑到位

    接触通用权限已经一年,现在使用已经很熟练,分享通用权限管理下面的一些好的开发思想. 安全漏洞对于一个小项目来说,可能不是特别的重视,对于一个大项目来说,这是特别重要需要注意的,特别是在项目开发中的就要 ...

  2. csharp: MVC Controls

    http://mvccontrolstoolkit.codeplex.com/ MVC Controls Toolkit http://mvcjquerycontrols.codeplex.com/  ...

  3. csharp: Export or Import excel using NPOI

    excel 2003: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  4. Python基础(一),Day1

    python的安装 python2.x与3.x的部分区别 第一个python程序 变量 字符编码 注释 格式化字符串 用户输入 常用的模块初始 if判断 循环语句 作业 1.python的安装 可以在 ...

  5. Wechat4j之Hello world——使用wechat4j快速开发java版微信公众号

    Wechat4j是一个开源的java微信开发框架,是目前最简单易用的java微信开发框架. 项目地址:https://github.com/sword-org/wechat4j Wechat4j.ja ...

  6. 备份一张iPhone拍照写入exif中的orientation图片

  7. [经验][JS]如何观察网站,进而模仿

    应该存在着一类人: 1.看到美丽的网站时,就会F12,看看他是怎么实现的 2.看到网站数据是自己需要的时候,就会F12,看看他是怎么拿到数据的 3.看到网站一个有趣的模块时,,就会F12,看看他是怎么 ...

  8. 微信公共平台开发5 .net

    每次在于微信交互时,都要用到access_token,但是这个值限制的是有时间的,但是access_token,在以后的高级功能里面会经常用到,所以这里不得不这里对前面所讲解的access_token ...

  9. css设置height 100%

    需要显式设置html,body为100%,body是相对于html,wrapper是相对于body html,body{ height: 100%; } .wrapper{ height: 100; ...

  10. 为什么Android应该根据屏幕分辨率来加载不同的图片文件

    1.图片在xxhdpi,手机是hdpi的 我们有一个手机是hdpi的.我们还有一个图片,我们把他放在xxhdpi下.当手机显示的时候,系统会去hdpi中找,发现没有图片,最终在xxhpi中找到.终于找 ...