Hdoj 4249 A Famous equation DP

Source: Internet
Author: User


Dp:

DP[LEN][K][I][J] Again the Len bit, the first number Len is I, the second number Len is J, and the Len bit is K

Each one can be transferred from the back one, can carry also can not carry

A Famous equationTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): Accepted submission (s): 147


Problem descriptionmr. B writes an addition equation such as 123+321=444 in the blackboard after class. Mr. G removes some of the digits and makes it look like "1?3+?? 1=44? ". Here "?" denotes removed digits. After Mr. B realizes some digits is missing, he wants to recover them. Unfortunately, there may is more than one-to-complete the equation. For example "1?

A.?

1=44? "can be completed to" 123+321=444 "," 143+301=444 "and many other possible solutions. Your job is to determine the number of different possible solutions. 
inputeach test Case describes a" a "," with an equation like a+b=c which contains exactly one plus Si GN "+" and one equal sign "=" with some question mark "?" Represent missing digits. Assume A, B and C are non-negative integers, and the length of each number is no more than 9. In the other words, the equation would contain three integers less than 1,000,000,000. 
outputfor Each test case, display a", "a" and "a" and the number of possible solutions To recover the equation. 
sample Input

7+1?=1?? 1+?

1=22


Sample Output
Case 1:3case 2:1HintThere was three solutions for the first case:7+10=17, 7+11=18, 7+12=19there was only one Sol Ution for the second case:11+11=22note this 01+21=22 is not a valid solution because extra leading zeros was not allowed.

Sourcefudan Local Programming Contest 2012


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Stack>using namespace Std;typedef long long int ll;char cpp[200];int A[200],len1,b[200],len2,c[200],len3;    LL Dp[20][20][20][20];int Main () {int cas=1;        while (cin>>cpp) {len1=len2=len3=0;        memset (A,0,sizeof (a));        Memset (b,0,sizeof (b));        Memset (C,0,sizeof (c));        int N=strlen (CPP); int i;        Stack<char> Stk;                    for (i=0;i<n;i++) {if (cpp[i]== ' + ') {while (!stk.empty ()) { Char c=stk.top ();                    Stk.pop (); if (c!= '?

') a[len1++]=c-' 0 '; else A[len1++]=-1; } i++; Break } stk.push (Cpp[i]); } for (; i<n;i++) {if (cpp[i]== ' = ') {while (!stk.empty ()) {char c=stk.top (); Stk.pop (); if (c!= '?

') b[len2++]=c-' 0 '; else B[len2++]=-1; } i++; Break } stk.push (Cpp[i]); } for (; i<n;i++) Stk.push (Cpp[i]); while (!stk.empty ()) {char cc=stk.top (); Stk.pop (); if (cc!= '? ') c[len3++]=cc-' 0 '; else C[len3++]=-1; } for (int i=len1-1;i>0;i--) if (a[i]==0) len1--; else break; for (int i=len2-1;i>0;i--) if (b[i]==0) len2--; else break; for (int i=len3-1;i>0;i--) if (c[i]==0) len3--; else break; Memset (Dp,0,sizeof (DP)); len==0 for (int i=0;i<=9;i++) {if (a[0]==-1| | a[0]==i) for (int j=0;j<=9;j++) {if (b[0]==-1| | B[0]==J) for (int k=0;k<=9;k++) if (c[0]==-1| | C[0]==K) {if (k== (i+j)%10) dp[0][k][i][j]=1; }}}///lEn=1 ... for (int len=1;len<len3;len++) {for (int i=0;i<=9;i++) {if (LEN==LEN1-1&A mp;&i==0) continue; if (len>=len1&&i!=0) continue; if (a[len]==-1| | a[len]==i) for (int j=0;j<=9;j++) {if (len==len2-1&&j==0) Conti Nue if (len>=len2&&j!=0) continue; if (b[len]==-1| | B[LEN]==J) for (int k=0;k<=9;k++) {if (Len==len3-1&&amp ; k==0) continue; if ((i+j)%10!=k && ((i+j+1)%10!=k)) continue; if (c[len]==-1| | C[LEN]==K) {///No rounding if ((i+j)%10==k) {for (Int. ii=0;ii<=9;ii++) for (int jj=0;jj<=9;jj++) for (int kk=0;kk<=9;kk++) {if (II+JJ==KK) | | | (II+JJ+1==KK)) DP[LEN][K][I][J]+=DP[LEN-1][KK][II][JJ]; }}///With Carry if ((i+j+1)%10==k) {for (int ii=0;ii<=9;ii++) for (int JJ =0;jj<=9;jj++) for (int kk=0;kk<=9;kk++) {if ((ii+jj>=10) && (II+JJ)%10==kk) | | ((ii+jj+1>=10) && (ii+jj+1)%10==KK)) DP[LEN][K][I][J]+=DP[LEN-1][KK][II][JJ]; } } }}}}} LL ans=0; int Mx=max (Len1,max (LEN2,LEN3)); for (int i=0;i<=9;i++) for (int j=0;j<=9;j++) for (int k=0;k<=9;k++) if ((i+j==k) | | (i+j+1==k)) {if (mx==1&&i+j!=k) continue; ANS+=DP[MX-1][K][I][J]; } cout<< "Case" <<cas++<< ":" <<ans<<endl; memset (cpp,0,sizeof (CPP)); } return 0;}




Hdoj 4249 A Famous equation DP

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.