Quicksum-s.b.s.

Source: Internet
Author: User

Quicksum

Queation:

Given a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition was the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs is inserted, evaluate the sum as usual. For example, consider the string "a" (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" and which evaluates to 3. So, in this case, given "a minimum of 1 addition was required to get the number 3. As another example, consider "303" and a target sum of 6. The best strategy isn't "3+0+3", but "3+03". You can do this because leading zeros does 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 a expression from numbers that evaluates to sum. If This is impossible, return-1.

Example

"382834"

100

Returns:2

There is 3 ways to get 100. They is 38+28+34, 3+8+2+83+4 and 3+82+8+3+4. The minimum required is 2.

Constraints

-numbers'll contain between 1 and characters, inclusive.

-Each character in numbers would be a digit.

-sum would be between 0 and inclusive.

-the string would be shorter than.

---------------------------------------------I'm a split line--------------------------------------------------------------

There are many ways to do this, such as "Memory Search + pruning", but I use DP.

Because the data is too weak (the plus number is less than 10, and not greater than 100 ...). So open a three-dimensional array dp[i][j][k].

I, J denote the number of strings from I to J;

K means at this time and for K;

The number of plus signs to be stored in the array.

Not much to say, on the code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <cmath>6#include <algorithm>7#include <cstdlib>8 using namespacestd;9 intCutstring,int,int);Ten intRead () { One     intx=0, f=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -     returnx*F; the } - Long Longchang=0; - intdp[101][101][101]; - intMain () + { -     stringSLong LongSumLong Longans=0; +Cin>>s; Achang=s.length (); atCin>>sum; -      for(intI=0; i<= -; i++) -       for(intj=0; j<= -; j + +) -        for(intk=0; k<= -; k++) -dp[i][j][k]= One; - //cout<<dp[0][chang-1][sum]<<endl; in      for(intI=0; i<chang;i++) -       for(intj=0; i+j<chang;j++) to      { +         Long LongNum=cut (s,i,i+j); -         if(num<=sum) dp[i][i+j][num]=0; the      }  * //cout<<dp[0][chang-1][sum]<<endl; $      for(intI=1; i<chang;i++)//several longPanax Notoginseng       for(intHead=0; head+i<chang;head++)//starting position -        for(intj=0; j<=sum;j++)//and the the         for(intk=head;k//Plus bit +          for(intss=0;j-ss>0; ss++)//Intermediate and ADp[head][head+i][j]=min ((dp[head][k][j-ss]+dp[k+1][HEAD+I][SS]) +1, dp[head][head+i] [j]); theans=dp[0][chang-1][sum]; +     if(ans== One) ans=-1; -cout<<ans; $     return 0;  $ } - intCutstringSintAintb) - { the     Long Longn=0; -      for(inti=a;i<=b;i++)Wuyin=n*Ten+ (s[i]-'0'); the     returnN;  -}

Quicksum-s.b.s.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.