Quicksum-S.B.S.
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.的更多相关文章
- [字符哈希] POJ 3094 Quicksum
Quicksum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16488 Accepted: 11453 Descri ...
- Quicksum -SilverN
quicksum Given a string of digits, find the minimum number of additions required for the string to e ...
- 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 ...
- H - Quicksum(1.5.3)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
- HDU.2734 Quicksum
Quicksum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- POJ 3094 Quicksum 难度:0
http://poj.org/problem?id=3094 #include<iostream> #include <string> using namespace std; ...
- poj 3094 Quicksum
#include <stdio.h> #include <string.h> ]; int main() { ; int i,len; while(gets(word)) { ...
随机推荐
- 不可或缺 Windows Native (5) - C 语言: 数组
[源码下载] 不可或缺 Windows Native (5) - C 语言: 数组 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 数组 示例cArray.h #ifn ...
- csharp: MongoDB
安装配置: Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-o ...
- DP入门---Robberies
HDU 2955 Description The aspiring Roy the Robber has seen a lot of American movies, and knows that ...
- Android SDK Tools和Android SDK Platform-tools
SDK Platform 可以理解为版本,因此有 SDK Platform 7,SDK Platform 8等等Android SDK Tools 是各个版本都可通用的工具文件夹,里面有draw9pa ...
- CSS中background背景色的作用范围
在div中设置背景色:当border宽度很大时就要考虑一个问题,那就是背景的作用范围,是包括边框呢?还是不包括呢?很明显,又到浏览器产生分歧的时候了. 在IE中背景色的作用范围为:content+pa ...
- CSS后代选择器,子选择器和相邻兄弟选择器
平时在代码练习中,经常用到后代选择器,子选择器也会用到,这里做个总结: 1,后代选择器和子选择器区别: ①写法不一样:后代选择器的标识为:空格 如:ul li{width:150px;} [ul和li ...
- .NET破解之太乐地图下载器【非暴破】
不知不觉,接触破解逆向已经三个月了,从当初的门外汉到现在的小白,这个过程只有经历过才知道其中的苦与乐: 有无知.困惑.痛苦.惊喜.彻悟.欣慰…… 有无助的软件脱壳,茫然的代码分析,有无趣的反复测试, ...
- iOS开发工程师面试知识点汇总
1.KVO实现原理 2.内存管理 3.Runtime 4.GCD 5.Block 6.响应者链 7.@peoperty属性特性 8.单元格cell加载图片处理
- 插入排序(java版)
public class InsertSortTest{ public static void InsertSort(int[] source) { //默认第一个元素已排序 for (int i = ...
- UVa 107 - The Cat in the Hat (找规律,注意精度)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...