HDU-3886-Final Kichiku “Lanlanshu”(数位DP)

Final Kichiku “Lanlanshu”

Problem Description

During 2010 summer training, temperlisyer often does problem like this:
“Consider a decimal integer as sequence of digits {D0, D1 … Dn-1} (D0 > 0), if exists such x, y and z, satisfying:
1.Di-1<Di (0<i<=x)
2.Di-1=Di (x<i<=y)
3.Di-1<Di (y<i<=z)
4.Di-1>Di (z<i<=n-1)

We call this integer “Lanlanshu”, now give you two numbers A and B, calculate how many “Lanlanshu” are in [A, B].“
He solved so many of these and finally get bored, and then get crazy! He decided to make up a problem to put this type of problems to an end.
Give you a string str consists only by ‘/’, ‘-‘ and ‘\’, and its length is l. Consider a decimal integer as sequence of digits {D0, D1 … Dn-1} (D0 > 0), define x0=0, xl=n-1, if exists such x1, x2…xl (x0 < x1 < x2 < … < xl) satisfying:

  1. If str[i]=’/’, Dj-1<Dj (xi<j<=xi+1)
  2. If str[i]=’-’, Dj-1=Dj (xi<j<=xi+1)
  3. If str[i]=’\’, Dj-1>Dj (xi<j<=xi+1)

We call it Final Kichiku “Lanlanshu”, now give you two numbers A and B, calculate how many Final Kichiku “Lanlanshu” are in [A, B]. This number maybe huge, we only want to now the last 8 digits of the result.

Input

Multiple cases (no more than 100), for each case:
The first line is string str, length is below 100.
The second line contains two integers A and B (0≤Ai≤Bi≤10^100).
Input terminates by EOF.

Output

For each case, output 8 digits representing the last 8 digits of the number of Final Kichiku “Lanlanshu” in [A, B]. If it’s less than 8 digits, fill it with leading zeros.

Sample Input

/\
01221 2012
 

Sample Output

00000315

解题思路:

又是一个明显的数位DP。
但是写了好久都没得出正确答案。
最后还是参考一小部分代码。成功A掉这题。
题意是要求输出AB区间内形如给定的字符串的数的个数。
在枚举数位的时候。
dp[pos][pre][sta] pos记录当前枚举到第几位,pre记录前一位的值。sta记录枚举到pos的位置时对应给定的字符串的哪个位置。
在枚举时,需要注意的点有这么几个:
首先是贪心的当枚举到第pos位置时,如果可以进入下一个起伏就先进入。如果不能则再判断。
而如果有前导零,那么时不算在起伏里面的,也就是从一个不是前导0的位置出开始计算起伏。
最后因为题目给的数据很大。只能用字符串存。我们知道枚举A-B的区域是是 res(b)- res(a-1) 的值,所以要自己写一个大数减法的运算。

AC代码:

#include <bits/stdc++.h>
#define int long long
const int N = 1e6+10;
using namespace std;
const int mod = 1e8;
int a[111];
int dp[111][12][111];
int len;
string s,s1,s2;

bool check ( int x , int y , char c ) // 判断是否需要更新状态
{
    if ( c == '/' &&  x < y ) return true ;
    if ( c == '-' && x == y ) return true ;
    if ( c == '\\' && x > y ) return true ;
    return false ;
}

int dfs(int pos,int pre,int sta,int limit,int lead)
{
    if(pos == -1) return (sta == len-1) ? 1 : 0;
    if(!limit && !lead && dp[pos][pre][sta] != -1) return dp[pos][pre][sta];
    int up = limit ? a[pos] : 9;
    int tmp = 0;
    for(int i = 0 ; i <= up ; i ++)
    {
        if ( lead ) tmp=(tmp+dfs(pos-1,i,sta,limit&&i==a[pos],lead&&i==0))%mod;
        else if ( sta<len-1 && check(pre,i,s[sta+1]) ) tmp=(tmp+dfs(pos-1,i,sta+1,limit&&i==a[pos],lead))%mod;
        else if ( sta>=0 && check(pre,i,s[sta]) ) tmp=(tmp+dfs(pos-1,i,sta,limit&&i==a[pos],lead))%mod;
    }
    if(!limit && !lead) dp[pos][pre][sta] = tmp;
    return tmp;
}

int res(string n,int x)
{
    int pos = 0;
    int tmp = 0;
    int len1 = n.size();
    while(tmp < len1 && n[tmp] == '0') tmp ++;
    if(tmp == len1) return 0;
    // 处理字符串,映射到a数组里去
    for(int i = len1-1 ; i >= tmp; i --)
        a[pos++] = n[i]-'0';
    if(x) // 大数减 1
    {
        int pos1 = 0;
        while(a[pos1] == 0) pos1 ++;
        for(int i = pos1 ; i >= 0; i --)
            a[i] = (a[pos1]-1+10)%10;
    }
    return dfs(pos-1,0,-1,1,1)%mod;
}

signed main()
{
    int t;
    while(cin>>s)
    {
        memset(dp,-1,sizeof(dp));
        len = s.size();
        int a,b;
        cin>>s1>>s2;
        int tmp1 = res(s2,0);
        int tmp2 = res(s1,1);
        printf("%08lld\n",(tmp1-tmp2+mod)%mod);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值