quicksum

Queation:

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.

---------------------------------------------我是分割线--------------------------------------------------------------

本题有多种方法,例如“记忆化搜索+剪枝”,但我用的是DP。

由于数据太弱(加号数小于10,和不大于100……)所以开个三维数组dp[i][j][k]。

i、j表示字符串从i开始到j表示的数;

k表示此时和为k;

数组内存放所需加号数。

不多说,上代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int cut(string,int,int);
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
long long chang=;
int dp[][][];
int main()
{
string s;long long sum;long long ans=;
cin>>s;
chang=s.length();
cin>>sum;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++)
dp[i][j][k]=;
// cout<<dp[0][chang-1][sum]<<endl;
for(int i=;i<chang;i++)
for(int j=;i+j<chang;j++)
{
long long num=cut(s,i,i+j);
if(num<=sum) dp[i][i+j][num]=;
}
// cout<<dp[0][chang-1][sum]<<endl;
for(int i=;i<chang;i++) //数长
for(int head=;head+i<chang;head++) //始位
for(int j=;j<=sum;j++) //和
for(int k=head;k<head+i;k++) //加号位
for(int ss=;j-ss>;ss++) //中间和
dp[head][head+i][j]=min((dp[head][k][j-ss]+dp[k+][head+i][ss])+,dp[head][head+i][j]);
ans=dp[][chang-][sum];
if(ans==) ans=-;
cout<<ans;
return ;
}
int cut(string s,int a,int b)
{
long long n=;
for(int i=a;i<=b;i++)
n=n*+(s[i]-'');
return n;
}

Quicksum-S.B.S.的更多相关文章

  1. [字符哈希] POJ 3094 Quicksum

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

  2. Quicksum -SilverN

    quicksum Given a string of digits, find the minimum number of additions required for the string to e ...

  3. ACM——Quicksum

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

  4. Quicksum

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

  5. POJ3094 Quicksum

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

  6. TJU Problem 2520 Quicksum

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

  7. H - Quicksum(1.5.3)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

  8. HDU.2734 Quicksum

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

  9. POJ 3094 Quicksum 难度:0

    http://poj.org/problem?id=3094 #include<iostream> #include <string> using namespace std; ...

  10. poj 3094 Quicksum

    #include <stdio.h> #include <string.h> ]; int main() { ; int i,len; while(gets(word)) { ...

随机推荐

  1. 【JS复习笔记】02 对象与函数

    好吧,因为很重要的事情,几天没写笔记了. 关于对象: ||可以用来填充默认值,如:myApp.name || "无" &&可以用来避免错误,myApp.NameOb ...

  2. 多余的Using Namespaces或引用会影响程序的执行效率么?

    在.NET程序编写中,需要using相应命名空间或添加相应的References,可有时候没有使用到的命名空间也被添加到了Using Namespaces中,那么,这样会影响程序的执行效率么? 通过示 ...

  3. 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Ap ...

  4. 老毛桃安装Win8(哪里不会点哪里,so easy)

    先来一张美女图,是不是很漂亮呢!继续往下看! 英雄不问出路,美女不看岁数!求推荐啊! 每次碰到妹子找我装系统的时候我都毫不犹豫的答应了,心里暗暗想到:好好表现啊!此刻的心情比见家长还要激动和紧张! 有 ...

  5. 机器学习实战 - 读书笔记(12) - 使用FP-growth算法来高效发现频繁项集

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第12章 - 使用FP-growth算法来高效发现频繁项集. 基本概念 FP-growt ...

  6. Hibernate的初步

    1.简介 在java开发领域,基于数据库应用的设计与实现一直都是面向关系的,Hibernate对象/关系映射ORM框架的出现为java面向对象开发提供了易于使用的数据持久化解决方案. ORM介绍: ( ...

  7. 由简入繁实现Jquery树状结构

    在项目中,我们经常会需要一些树状结构的样式来显示层级结构等,比如下图的样式,之前在学.net的时候可以直接拖个服务端控件过来直接使用非常方便.但是利用Jquery的一些插件,也是可以实现这些效果的,比 ...

  8. python 学习之电脑的发展历史

    电脑的发展历史 电脑的学名叫计算机,电脑是用来做计算的.在古时候,人们最早使用的计算工具可能是手指,英文单词“digit”既有“数字”的意思,又有“手指“的意思.古人用石头打猎,所以还有可能是石头来辅 ...

  9. SQL Server SQL语句执行顺序

    执行顺序: 1.FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1 2.ON:对vt1表应用ON筛选器只有满足 为真的行才被插入vt2 3.OUTER(join):如果指定了 OUTER ...

  10. Webform(简单控件、复合控件)

    一.简单控件: 1.label控件 <asp:Label ID="Label1" runat="server" Text="账 号:" ...