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. webpack常用的插件安装命令

    webpack常用的插件安装命令:1:npm install html-webpack-plugin --save-dev //自动快速的帮我们生成HTML.2:npm install css-loa ...

  2. 养只爬虫当宠物(Node.js爬虫爬取58同城租房信息)

    先上一个源代码吧. https://github.com/answershuto/Rental 欢迎指导交流. 效果图 搭建Node.js环境及启动服务 安装node以及npm,用express模块启 ...

  3. JS控制HTML元素的显示和隐藏

    JS控制HTML元素的显示和隐藏 利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一: 1 2 ...

  4. SAP数据更新的触发

    SAP 应用系统架构         应用层运行着DIALOG进程,每个DIALOG进程绑定一个数据库进程,DIALOG进程与GUI进行通信,每次GUI向应用服务器发送请求时都会通过dispatche ...

  5. 如何利用ArcGIS Engine接口实现打开Raster Catalog中的某一幅指定的影像?

    将IRasterCatalog转化为ITable,然后通过ITable.GetRow返回指定索引的IRow,将IRow转为IRasterCatalogItem,进而获取IRasterCatalogIt ...

  6. WebActivatorEx 注入时的使用

    WebActivator类库提供了3种功能,允许分别在Application_Start初始化之前,之后以及ShutDown的时候,分别执行指定的代码,并且允许多次指定.示例如下: [assembly ...

  7. 利用在线工具根据JSon数据自动生成对应的Java实体类

    如果你希望根据JSon数据自动生成对应的Java实体类,并且希望能进行变量的重命名,那么“JSON To Java”一定适合你.(下面的地址需要FQ) https://jsontojava.appsp ...

  8. 利用UIScrollView实现几个页面的切换

    此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容: 原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514 效果图如下 ...

  9. 【读书笔记】iOS网络-HTTP-URL结构

    http://user:password@hostname:port/absolute-path?query. http:  协议 user:password@   认证 hostname:  主机名 ...

  10. 【原】训练自己haar-like特征分类器并识别物体(2)

    在上一篇文章中,我介绍了<训练自己的haar-like特征分类器并识别物体>的前两个步骤: 1.准备训练样本图片,包括正例及反例样本 2.生成样本描述文件 3.训练样本 4.目标识别 == ...