HDOJ 4249 A Famous Equation DP
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
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.
number is no more than 9. In the other words, the equation will contain three integers less than 1,000,000,000.
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 <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的更多相关文章
- HDU 4249 A Famous Equation(数位DP)
题目链接:点击打开链接 思路:用d[i][a][b][c][is]表示当前到了第i位, 三个数的i位各自是a,b,c, 是否有进位 , 的方法数. 细节參见代码: #include<cstdio ...
- HDU-4249-A Famous Equation(DP)
Problem Description Mr. B writes an addition equation such as 123+321=444 on the blackboard after cl ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- 性能测试之LoardRunner 结果分析
性能结果分析是性能测试中的重中之重,也是难点所在,以下总结了看图的一些顺序: 1.首先可以检查Analysis模块提供的Summary Report,整个测试过程中我们所关心的各业务 2.首先关注性能 ...
- Android程序检测网络是否可用
在做Android应用程序中,连接网络的时候,常常要用到检测网络状态是否可用,在这里分享一个比较好用的方法. 本人参考:http://blog.csdn.net/sunboy_2050/article ...
- CButtonEx的实现
要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制.这可以通过定义一个以CButton为基类的新按钮类来实现.以下为具体的实现方法: 方法一: 加入一个新类,类名:CB ...
- 14.9.4 COMPACT and REDUNDANT Row Formats
14.9.4 COMPACT and REDUNDANT Row Formats InnoDB 早期的版本 使用一种未命名的文件格式(现在称为Antelope(羚羊)) 对于数据库文件 在这种文件格式 ...
- js获取上传文件的绝对路径
在html中 <input type="file" id="importFile" /> <input type="bu ...
- UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟
题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求 ...
- 漫谈并发编程(六):java中一些经常使用的并发构件的介绍
CountDownLatch 它被用来同步一个或多个任务,强制它们等待其他任务运行的一组操作完毕. 你能够向CountDownLatch对象设置一个初始计数值,不论什么在这个对象上 ...
- 两个Hacker,专门Patch TObject
http://hallvards.blogspot.fr/2006/03/hack-8-explicit-vmt-calls.html http://www.deltics.co.nz/blog/po ...
- Citrix 服务器虚拟化之三十一 XenApp 6.5负载均衡
Citrix 服务器虚拟化之三十一 XenApp 6.5负载均衡 说明: 环境基于实验三十 1.准备一台Windows Server 2008 R2的虚拟机名为XenAPP2,然后加入域k ...
- cronjob不跑得原因
能是环境的不同,能够在cronjob中加个env > /tmp/env.output查看 应用要同一时候输出标准错误合标准输出到一个文件能够&> /tmp/t