Quicksum -SilverN
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的更多相关文章
- [字符哈希] POJ 3094 Quicksum
Quicksum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16488 Accepted: 11453 Descri ...
- NOIP2010提高组乌龟棋 -SilverN
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- NOIP2010提高组 关押罪犯 -SilverN
(洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...
- NOIP2008提高组(前三题) -SilverN
此处为前三题,第四题将单独发布 火柴棒等式 题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0 ...
- NOIP2008普及组 题解 -SilverN
T1 ISBN号码 题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符, 其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符( ...
- 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 ...
随机推荐
- 【JS复习笔记】04 数组
JS里的数组其实并不是一个数组,它其实是一个对象,a[1]这种调用方式其实就是一个字面量为1的属性. 因为这东西实际上是一个对象,所以你就可以理解下面这种声明了吧! var arrName=['我可以 ...
- ASP.NET MVC进阶二
一.数据验证 数据验证的步骤 在模型类中添加与验证相关的特性标记 在客户端导入与验证相关的js文件和css文件 使用与验证相关的Html辅助方法 在服务器端判断是否通过服务器端验证 常用的验证标记 R ...
- 内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统
linux内核最小系统,使用内核版本:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.8.1.tar.bz2 1,FL2440板子的基本硬件: ...
- php中的不常用数组函数(一)(数组中元素的键和值对调 array_flip())
array_flip($arr); //交换数组中的键和值. //如下所示,如果$arr中有相同的值.交换之后 会被旧的覆盖,最后一个有效. /***********array_flip(交换数组中的 ...
- Convert string to binary and binary to string in C#
String to binary method: public static string StringToBinary(string data) { StringBuilder sb = new S ...
- 【GOF23设计模式】建造者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]建造者模式详解类图关系 建造飞船 package com.test.Builder; public class AirShi ...
- 模拟Select-Options对象实现多项数据输入功能
模拟Select-Options对象实现多项数据输入功能 Select-Options对象可以同时输入多项值并将所输入数据存入内表以供程序使用,不过Select-Options的功能有一定的局限 ...
- centos如何安装软件
背景 之前用的linux操作系统移植都是ubuntu,没有用过redhat版本的linux,最近开始想学习redhan版本的linux,就从centos开始.在安装完centos以后,第一个碰到的问题 ...
- iptables基本操作
一.基本操作 #启动防火墙 service iptables start #停止防火墙 service iptables stop #重启防火墙 service iptables restart #查 ...
- 【转】IOS设备旋转的内部处理流程以及一些【优化建议】
加速计是整个IOS屏幕旋转的基础,依赖加速计,设备才可以判断出当前的设备方向,IOS系统共定义了以下七种设备方向: typedef NS_ENUM(NSInteger, UIDeviceOrienta ...