您的位置:首页 > 其它

【数位dp】hud 2089 不要62 hdu 3555 Bomb

2017-05-17 22:01 337 查看
这是碰到的第一类数位dp:找这个数是否存在子串为一个一位数,或两位数

其实都一样,记录已经满足前几位即可。

特定的:

如果前面计算对后面有影响,计算前驱的值ten[]

hud 2089

初学,根据我自己的理解码代码,无限wa,不知道是错在哪里,后来强行手跑代码,发现先前的理解不准确。

数位dp的记忆化搜索不能理解为边爆搜边记录,这样是没有意义的(之前脑子懵)。

它的搜索只是搜了(0000~0009),记录到dp数组,然后根据其搜10次(001X~009X),以此类推。

wa代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
typedef long long LL;
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int INF=0x3f3f3f3f;
const int N = 20;
const double eps = 1e-6;

int dp
; //0->10^i-1
int ten
,f
;
void init()
{
ten[0] = 1;
for(int i = 1; i < 10; i++)
ten[i] = ten[i-1]*10;
}
int dfs(int pos,int num,int limit,int pre)
{
if(pos == -1)
return num;
if(!limit && dp[pos] != -1)
{
if(num) return ten[pos+1];
else    return dp[pos+1];
}
int top = limit ? f[pos]:9;
//printf("pos = %d top = %d\n",pos,top);
int ans = 0;
for(int i = 0; i <= top; i++)
ans += dfs(pos-1,num||(i==4)||(i==2&&pre==6),limit && i==f[pos],i);
if(!limit)
dp[pos+1] = ans;
return ans;
}

int main()
{
init();
int a,b;
while(~scanf("%d%d",&a,&b) && a,b)
{
memset(dp,-1,sizeof(dp));
int pos = 0;
int aa = a-1;
while(aa)
{
f[pos++] = aa%10;
aa /= 10;
}
int suma = dfs(pos-1,0,1,0);
//printf("%d\n",suma);
memset(dp,-1,sizeof(dp));
pos = 0;
int bb = b;
while(bb)
{
f[pos++] = bb%10;
bb /= 10;
}
int sumb = dfs(pos-1,0,1,0);
printf("%d\n",b-a+1-(sumb-suma));
}
return 0;
}


ac代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
typedef long long LL;
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int INF=0x3f3f3f3f;
const int N = 20;
const double eps = 1e-6;

int dp
[3]; //0->10^i-1 dp[i][2]表示有4或有62的个数,dp[i][1]表示首位有6的个数,dp[i][0]表示没有任何不吉利特征的个数
int ten
,f
;
void init()
{
ten[0] = 1;
for(int i = 1; i < 10; i++)
ten[i] = ten[i-1]*10;
}
int dfs(int pos,int num,int limit)
{
if(pos == 0)
return num==2;
if(!limit && dp[pos][num] != -1)
return dp[pos][num];
int top = limit ? f[pos]:9;
int ans = 0;
for(int i = 0; i <= top; i++)
{
if(num == 2 || (num==1&&i==2) || i==4)
ans += dfs(pos-1,2,limit&&i==f[pos]);
else if(i == 6)
ans += dfs(pos-1,1,limit&&i==f[pos]);
else
ans += dfs(pos-1,0,limit&&i==f[pos]);
}
if(!limit)
dp[pos][num] = ans;
return ans;
}

int solve(int a)
{
int pos = 0;
while(a)
{
f[++pos] = a%10;
a /= 10;
}
return dfs(pos,0,1);
}
int main()
{
init();
int a,b;
memset(dp,-1,sizeof(dp));
while(~scanf("%d%d",&a,&b) && a,b)
{
int suma = solve(a-1);
int sumb = solve(b);
printf("%d\n",b-a+1-(sumb-suma));
}
return 0;
}

同一类型 hud 3555

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<queue>
#include<cmath>
#include<algorithm>
#include<deque>
typedef long long LL;
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int INF=0x3f3f3f3f;
const int N = 25;
const double eps = 1e-6;

LL dp
[3]; //0->10^i-1   dp[i][2]表示49的个数,dp[i][1]表示首位是4的个数,dp[i][0]表示不包含49和4的个数
LL ten
;
int f
;

//void init()
//{
//    ten[0] = 1;
//    for(int i = 1; i < N; i++)
//        ten[i] = ten[i-1]*10;
//}
LL dfs(int pos,int zt,int limit)
{
if(pos == 0)
return zt==2;
if(!limit && dp[pos][zt] != -1) {
return dp[pos][zt];
}
int top = limit ? f[pos]:9;
LL ans = 0;
for(int i = 0; i <= top; i++)
{
if(zt==2 || (zt==1&&i==9))
ans += dfs(pos-1,2,limit && i==f[pos]);
else if(i==4)
ans += dfs(pos-1,1,limit && i==f[pos]);
else
ans += dfs(pos-1,0,limit && i==f[pos]);
}
if(!limit)
dp[pos][zt] = ans;
return ans;
}

LL solve(LL a)
{
int pos = 0;
while(a)
{
f[++pos] = a%10;
a /= 10;
}
return dfs(pos,0,1);
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--)
{
LL n;
scanf("%I64d",&n);
printf("%I64d\n",solve(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: