zoukankan      html  css  js  c++  java
  • HDU 6148 Valley Numer (数位DP)题解

    思路:

    只要把status那里写清楚就没什么难度T^T,当然还要考虑前导零!

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int N = 100000+5;
    const int MOD = 1000000007;
    int dp[110][15][5]; //不知0,升1,降2,降升3
    int a[N];
    ll dfs(ll pos,int pre,int sta,bool limit,bool lead){
        if(pos == -1) return lead? 0 : 1;
        if(!limit && dp[pos][pre][sta] != -1) return dp[pos][pre][sta];
        int top = limit? a[pos] : 9;
        ll ret = 0;
        int sta2;
        for(int i = 0;i <= top;i++){
            if(sta == 0){
                if(lead) sta2 = 0;
                else if(i > pre) sta2 = 1;
                else if(i < pre) sta2 = 2;
                else sta2 = 0;
            }
            else if(sta == 1){
                if(i < pre) continue;
                else sta2 = 1;
            }
            else if(sta == 2){
                if(i > pre) sta2 = 3;
                else sta2 = 2;
            }
            else if(sta == 3){
                if(i >= pre) sta2 = 3;
                else continue;
            }
            ret += dfs(pos-1,i,sta2,limit && i == top,lead && i == 0);
            ret %= MOD;
        }
        ret %= MOD;
        if(!limit && !lead) dp[pos][pre][sta] = ret;
    
        return ret;
    }
    int main(){
        int T;
        char s[105];
        memset(dp,-1,sizeof(dp));
        scanf("%d",&T);
        while(T--){
            scanf("%s",s);
            int len = strlen(s),pos = 0;
            for(int i = len - 1;i >=0 ;i--){
                a[pos++] = s[i] - '0';
            }
            printf("%lld
    ",dfs(pos-1,0,0,true,true));
        }
        return 0;
    }
    


  • 相关阅读:
    achivemq(消息队列)的使用
    java高并发当时处理的思路
    字符串的应用
    正则表达式
    文本文件的读取与写入
    继承
    冒泡排序法
    类与对象
    数据类型
    关键字和语句
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9408788.html
Copyright © 2011-2022 走看看