标签:des style blog http color io os ar for
7+1?=1? ?1+?1=22
Case 1: 3 Case 2: 1HintThere are three solutions for the first case: 7+10=17, 7+11=18, 7+12=19 There is only one solution for the second case: 11+11=22 Note that 01+21=22 is not a valid solution because extra leading zeros are not allowed.
#include<stdio.h> #include<string.h> char a[50],b[50],c[50],s[100]; long long dp[10][2]; int main() { int la,lb,lc,i,j,k,now,p,q,jp,jq,cas=1; while(~scanf("%s",s)) { for(i=0;s[i];i++) if(s[i]=='+' || s[i]=='=') s[i]=' '; sscanf(s,"%s%s%s",a,b,c); la=strlen(a)-1; lb=strlen(b)-1; lc=strlen(c)-1; dp[0][0]=1; dp[0][1]=0; now=1; while(la>=0 || lb>=0 || lc>=0) { p=q=jp=jq=0; if(la>=0 && a[la]=='?') { for(i=0;i<=9;i++) { if(!la && now>1 && !i) continue; if(lb>=0 && b[lb]=='?') { for(j=0;j<=9;j++) { if(!lb && now>1 && !j) continue; if(lc>=0 && c[lc]=='?') { for(k=0;k<=9;k++) { if(!lc && now>1 && !k) continue; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } else { if(lc>=0) k=c[lc]-'0'; else k=0; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } } else { if(lb>=0) j=b[lb]-'0'; else j=0; if(lc>=0 && c[lc]=='?') { for(k=0;k<=9;k++) { if(!lc && now>1 && !k) continue; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } else { if(lc>=0) k=c[lc]-'0'; else k=0; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } } } else { if(la>=0) i=a[la]-'0'; else i=0; if(lb>=0 && b[lb]=='?') { for(j=0;j<=9;j++) { if(!lb && now>1 && !j) continue; if(lc>=0 && c[lc]=='?') { for(k=0;k<=9;k++) { if(!lc && now>1 && !k) continue; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } else { if(lc>=0) k=c[lc]-'0'; else k=0; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } } else { if(lb>=0) j=b[lb]-'0'; else j=0; if(lc>=0 && c[lc]=='?') { for(k=0;k<=9;k++) { if(!lc && now>1 && !k) continue; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } else { if(lc>=0) k=c[lc]-'0'; else k=0; if(i+j==k) p++; if(i+j==k-1) jp++; if(i+j-10==k) q++; if(i+j-10==k-1) jq++; } } } dp[now][0]=dp[now-1][0]*p+dp[now-1][1]*jp; dp[now][1]=dp[now-1][0]*q+dp[now-1][1]*jq; now++; la--; lb--; lc--; } printf("Case %d: %I64d\n",cas++,dp[now-1][0]); } }
HDU-4249-A Famous Equation(DP)
标签:des style blog http color io os ar for
原文地址:http://blog.csdn.net/faithdmc/article/details/40361631