Hdu 3555 - Bomb (数位dp)

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3555

题目大意:
给定一个 N ,找出1N间含有子串49的数字个数

分析:
这个和不要62类似,但是略有不同,现在找的是含有49的数,所以一样记一个参数pre,代表前一个数位的值,然后记一个sta,这个和不要62中的sta有些不同,有三重状态

  • sta = 0前一个数位不是6
  • sta = 1前一个数位是6
  • sta = 2之前出现过49
    显然最后 pos==1 的时候,只有 sta==2 的结果才能返回1

另外对于当sta=2时,当前位无论如何放置都不会改变sta的值,而对于其他情况,类似不要62

分析:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll dp[30][10];

int a[50],pos;

ll dfs(int pos,int pre,int sta,bool limit)
{
    if (pos==-1)
    {
        return sta==2;
    }

    if (!limit&&dp[pos][sta]!=-1)  {
        //printf("dp[%d][%d] = %lld\n",pos,sta,dp[pos][sta]);
        return dp[pos][sta];
    }
    int up = limit?a[pos]:9;
    ll ans = 0;
    for (int i = 0 ; i <= up ; i ++)
    {
        int temp = sta;
        if (pre==4&&i==9)
            temp = max(temp,2);
        else if (i==4)
            temp = max(temp,1);
        else
        {
            if (temp!=2)
                temp = 0;
        }
        ans += dfs(pos-1,i,temp,limit&&i==pos[a]);
    }
    if (!limit) dp[pos][sta] = ans;
    return ans;
}


ll solve(ll n)
{
    pos = 0;
    while (n)
    {
        a[pos++] = n%10;
        n /= 10;
    }
    return dfs(pos-1,0,0,true);
}

int main()
{
    ll n;
    int T;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while (T--)
    {
        scanf("%lld",&n);
        printf("%lld\n",solve(n));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值