Quicksum-S.B.S.
2016-04-05 18:32
435 查看
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;
数组内存放所需加号数。
不多说,上代码:
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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } long long chang=0; int dp[101][101][101]; int main() { string s;long long sum;long long ans=0; cin>>s; chang=s.length(); cin>>sum; for(int i=0;i<=100;i++) for(int j=0;j<=100;j++) for(int k=0;k<=100;k++) dp[i][j][k]=11; // cout<<dp[0][chang-1][sum]<<endl; for(int i=0;i<chang;i++) for(int j=0;i+j<chang;j++) { long long num=cut(s,i,i+j); if(num<=sum) dp[i][i+j][num]=0; } // cout<<dp[0][chang-1][sum]<<endl; for(int i=1;i<chang;i++) //数长 for(int head=0;head+i<chang;head++) //始位 for(int j=0;j<=sum;j++) //和 for(int k=head;k<head+i;k++) //加号位 for(int ss=0;j-ss>0;ss++) //中间和 dp[head][head+i][j]=min((dp[head][k][j-ss]+dp[k+1][head+i][ss])+1,dp[head][head+i][j]); ans=dp[0][chang-1][sum]; if(ans==11) ans=-1; cout<<ans; return 0; } int cut(string s,int a,int b) { long long n=0; for(int i=a;i<=b;i++) n=n*10+(s[i]-'0'); return n; }
相关文章推荐
- [LeetCode]334. Increasing Triplet Subsequence
- Android消息异步机制(ThreadLocal、MessageQueue、Looper、Handler关系总结)
- IOS开发-UI学习-UINavigationController(导航控制器)的使用
- TextruePacker
- iOS 隐藏导航栏UINavigationBar底部黑线
- 使用野狗(Wilddog)云setValue写入数据
- lintcode-medium-Segment Tree Query II
- 【iOS】使用UIView绘制本地验证码
- lintcode-medium-Segment Tree Query
- What's the difference between all the Selection Segues?
- 【iOS】UILabel多行文本的高度计算
- lintcode-medium-Segment Tree Build II
- lintcode-medium-Segment Tree Build
- Material Design UI Widgets —— Palette 调色板
- 大作业关于(“有爱”youi)的简介
- mysql5中大数据错误:Packet for query is too large (1117260 > 1048576). You can change this value on the ser
- 将UIDatePicker的日期显示为数字和中文
- Material Design UI Widgets —— CardView 卡片视图
- CodeForces 618B-Guess the Permutation【搜索】
- NSURLConnection的sendAsynchronousRequest