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 ...
随机推荐
- Python轻量Web框架Flask使用
http://blog.csdn.net/jacman/article/details/49098819 目录(?)[+] Flask安装 Python开发工具EclipsePyDev准备 Flask ...
- python学习笔记2(pycharm、数据类型)
Pycharm 的使用 IDE(Integrated Development Environ ment) :集成开发环境 Vim :经典的linux下的文本编辑器(菜鸟和大神喜欢使用) Emac ...
- mysql grant all on *.* to xxx@'%' 报Access denied for user 'root'@'localhost'
今日,开发反馈某台mysql服务器无法登陆,解决之后,远程登录后发现用户只能看到information_schema,其他均看不到. 故登录服务器执行: mysql> grant all on ...
- windows临界区
临界区: 临界区是一种轻量级机制,在某一时间内只允许一个线程执行某个给定代码段.通常在多线程修改全局数据时会使用临界区.事件.信号量也用于多线程同步,但临界区与它们不同,并不总是执行向内核模式的切换, ...
- Hazelcast介绍与使用
Hazelcast 是一个开源的可嵌入式数据网格(社区版免费,企业版收费).你可以把它看做是内存数据库,不过它与 Redis 等内存数据库又有些不同.项目地址:http://hazelcast.org ...
- H5实现的可自定义贪吃蛇游戏
原创游戏,使用lufylegend.js开发 用canvas实现的贪吃蛇游戏,与一般的贪吃蛇游戏不同,图片经过美工设计,代码设计支持扩展和自定义. 游戏元素丰富,包括障碍物(仙人掌),金币(奖励),苹 ...
- 获取经过跳转后的url地址
粗略一算,不写code已经好几个月了. 昨日受兄弟所托,为他写了一个小小的程序. 程序功能: 自动获取跳转后的Url地址 如下图所示: (newUrl.txt为转换后的地址信息...) 实现过程: 每 ...
- iOS 删除 Main.storyboard 和 LaunchScreen.storyboard
第一步: 右键选中Main.storyboard —- delete —— Move to Trash LaunchScreen同理 第二步 点击工程名,就是最顶级目录 右侧出现general选项卡 ...
- virt manager 提示权限不允许(ubuntu)
问题描述: 新安装virt manager 打开提示权限不允许(ubuntu 15.04); 提示检查libvirt-bin包是否安装:libvirtd服务是否已运行:当前用户是否在libvirtd组 ...
- UVa 100 - The 3n + 1 problem(函数循环长度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...