DP:

DP[len][k][i][j] 再第len位,第一个数len位为i,第二个数len位为j,和的第len位为k

每一位能够从后面一位转移过来,能够进位也能够不进位

A Famous Equation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 500    Accepted Submission(s): 147

Problem Description
Mr. B writes an addition equation such as 123+321=444 on 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 are missing, he wants to recover them.
Unfortunately, there may be more than one way to complete the equation. For example “1?

3+??

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.

 
Input
Each test case describes a single line with an equation like a+b=c which contains exactly one plus sign “+” and one equal sign “=” with some question mark “?” represent missing digits. You may 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 will contain three integers less than 1,000,000,000.
 
Output
For each test case, display a single line with its case number and the number of possible solutions to recover the equation.
 
Sample Input
7+1?=1?
?1+? 1=22
 
Sample Output
Case 1: 3
Case 2: 1
Hint
There 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.
 
Source
 

#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&&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) continue;
if(len>=len2&&j!=0) continue;
if(b[len]==-1||b[len]==j)
for(int k=0;k<=9;k++)
{
if(len==len3-1&&k==0) continue;
if(((i+j)%10!=k)&&((i+j+1)%10!=k))
continue;
if(c[len]==-1||c[len]==k)
{
///没有进位
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];
}
}
///有进位
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的更多相关文章

  1. HDU 4249 A Famous Equation(数位DP)

    题目链接:点击打开链接 思路:用d[i][a][b][c][is]表示当前到了第i位, 三个数的i位各自是a,b,c, 是否有进位 , 的方法数. 细节參见代码: #include<cstdio ...

  2. HDU-4249-A Famous Equation(DP)

    Problem Description Mr. B writes an addition equation such as 123+321=444 on the blackboard after cl ...

  3. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  4. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  5. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  6. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  7. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  8. HDOJ 1501 Zipper 【简单DP】

    HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...

  9. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. Web前端,高性能优化

    高性能HTML 一.避免使用iframe iframe也叫内联frame,可将一个HTML文档嵌入另一个HTML文档中. iframe的好处是,嵌入的文档独立于父文档,通常也借此使浏览器模拟多线程.缺 ...

  2. Swift - 多层无缝循环滚动背景(SpriteKit游戏开发)

    在游戏开发中,比如跑酷游戏.我们需要实现背景的无限循环滚动,来营造运动的效果.除了单层的背景滚动,还有视差滚动. 视差滚动是指让多层背景以不同的速度移动,形成立体的效果,从而带来非常出色的视觉体验. ...

  3. Tomcat详细用法学习(四)

    本篇接上一篇<Tomcat详细用法学习(三)>,主要讲解配置虚拟主机.打包web应用成war包和Tomcat的体系结构 对于Tomcat服务器,可以放置多个网站(多个web应用),这就是讲 ...

  4. Thinkphp入门 五 —模型 (49)

    原文:Thinkphp入门 五 -模型 (49) [数据库操作model模型] model  模型  数据库操作 tp框架主要设计模式:MVC C:controller   控制器   shop/Li ...

  5. CDialogBar(对话条)和CReBar(伸缩条)的编程

    对话条是工具栏和无模式对话框相结合的产物,对话条和对话框类似,包含有标准的Windows控件,并且可以通过创建对话框模板来表示对话条. 伸缩条功能很强大,我们可以在伸缩条中直接增加CToolBar,C ...

  6. Linux下限制Shell:Rssh和Scponly

    限制Shell,正如Rsh和Scponly让系统管理员限制Linux用户可以做哪些操作,你可以创建用户,将被允许通过Scp复制文件,但不会被允许登录到系统的命令行.这是非常重要的安全功能,应考虑每个系 ...

  7. Marshal UTF8 Strings in .NET

    原文:Marshal UTF8 Strings in .NET Marshal UTF8 Strings in .NET Wow, what a pain in the butt. .NET stri ...

  8. Mysql自增主键ID重新排序方法详解

    Mysql数据库表的自增主键ID号乱了,需要重新排列. 原理:删除原有的自增ID,重新建立新的自增ID. 1,删除原有主键: ALTER TABLE `table_name` DROP `id`; 2 ...

  9. Eclipse 修改maven 仓储Repository位置

    简述: 使用两个Nexus, 需要配置两份不同的Maven仓库 步骤: 1. 下载新的Maven运行包 2. 进入conf/ 修改setting.xml项 <localRepository> ...

  10. MySQL 更新走全表和索引的评估记录数

    #!/usr/bin/perl use DBI; $db_name='scan'; $ip='127.0.0.1'; $user="root"; $passwd="123 ...