【算法基础:动态规划】5.4 数位统计DP(计数问题)(数位DP)

文章介绍了如何使用数位动态规划模板解法来解决338.计数问题,提供了Java代码实现。同时提到了另一种解法,即通过分情况讨论来计算1到n中数字x出现的次数,但表示该方法尚未完全理解。文章附有相关链接供进一步学习。
摘要由CSDN通过智能技术生成

例题:338. 计数问题

https://www.acwing.com/problem/content/340/
在这里插入图片描述

解法1——转换成1067. 范围内的数字计数,数位DP模板

解法来自:【算法】数位DP

import java.util.*;

public class Main {
    static int[][] memo;
    static char[] s;
    static int t;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (true) {
            int a = sc.nextInt(), b = sc.nextInt();
            if (a == 0 && b == 0) return;
            for (int i = 0; i <= 9; ++i) {
                System.out.print((a > b? count(a, i) - count(b, i): count(b, i) - count(a, i)) + " ");
            }
            System.out.println();
        }
    }

    // 计算1~n中,数字x出现的次数
    static int count(int n, int x) {
        s = Integer.toString(n).toCharArray();
        t = x;
        int m = s.length;
        memo = new int[m][m];
        for (int i = 0; i < m; ++i) Arrays.fill(memo[i], -1);
        return dfs(0, true, false, 0);
    }
    
    static int dfs(int i, boolean isLimit, boolean isNum, int cnt) {
        if (i == s.length) return cnt;
        if (!isLimit && isNum && memo[i][cnt] != -1) return memo[i][cnt];

        int res = 0;
        if (!isNum) res = dfs(i + 1, false, false, 0);
        int up = isLimit? s[i] - '0': 9;
        for (int d = isNum? 0: 1; d <= up; ++d) {
            res += dfs(i + 1, isLimit && d == up, true, cnt + (d == t? 1: 0));
        }
        if (!isLimit && isNum) memo[i][cnt] = res;
        return res;
    }
}

解法2——分情况讨论(TODO,还没理解)

https://www.acwing.com/video/323/
https://www.acwing.com/activity/content/code/content/64211/

我们需要实现一个函数 count(int n, int x) 求 1 ~ n 中数字 x 出现的次数。

分情况讨论 是思路中最重要的部分。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 10;

/*

001~abc-1, 999

abc
    1. num[i] < x, 0
    2. num[i] == x, 0~efg
    3. num[i] > x, 0~999

*/

int get(vector<int> num, int l, int r)
{
    int res = 0;
    for (int i = l; i >= r; i -- ) res = res * 10 + num[i];
    return res;
}

int power10(int x)
{
    int res = 1;
    while (x -- ) res *= 10;
    return res;
}

int count(int n, int x)
{
    if (!n) return 0;

    vector<int> num;
    while (n)
    {
        num.push_back(n % 10);
        n /= 10;
    }
    n = num.size();

    int res = 0;
    for (int i = n - 1 - !x; i >= 0; i -- )
    {
        if (i < n - 1)
        {
            res += get(num, n - 1, i + 1) * power10(i);
            if (!x) res -= power10(i);
        }

        if (num[i] == x) res += get(num, i - 1, 0) + 1;
        else if (num[i] > x) res += power10(i);
    }

    return res;
}

int main()
{
    int a, b;
    while (cin >> a >> b , a)
    {
        if (a > b) swap(a, b);

        for (int i = 0; i <= 9; i ++ )
            cout << count(b, i) - count(a - 1, i) << ' ';
        cout << endl;
    }

    return 0;
}

相关链接⭐

【算法】数位DP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wei *

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值