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

题目大意:

给一定区间 \([A,B]\) ,一串由 /, \ , - 组成的符号串。求满足符号串要求的数字个数。

要求如下:

  • / 表示数字从左到右递增;
  • \ 表示数字从左到右递减;
  • - 表示数字从左到右相等。

第一状态 \(f[pos][id][pre][all0]\) 表示当前处在如下情况下的方案数:

  • 当前所在数位为 pos 位;
  • 当前数位对应字符串 s 的第 id 个元素 s[id]
  • 前一数位为 pre
  • all0 表示是不一直都是前导0。

开函数 dfs(int pos, int id, int pre, int all0, bool limit) 进行求解,其中:

  • posidpreall0 的含义和上述状态中的相同;
  • limit 表示当前是否处于闲置条件。

需要注意的是:

因为每组测试数据的字符串 s 都不一定相同,所以每组数据之前都要对 f 数组进行初始化(init())。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 100000000LL;
char s[110], in[110];
long long f[110][110][10][2];
int a[110];
int len;
void init() {
memset(f, -1, sizeof(f));
}
bool check(char c, int pre, int i) {
return c=='/'&&pre<i || c=='-'&&pre==i || c=='\\'&&pre>i;
}
long long dfs(int pos, int id, int pre, int all0, bool limit) {
if (pos < 0) return id == len;
if (!limit && f[pos][id][pre][all0] != -1) return f[pos][id][pre][all0];
int up = limit ? a[pos] : 9;
long long tmp = 0;
for (int i = 0; i <= up; i ++) {
if (all0) {
tmp = (tmp + dfs(pos-1, id, i, i==0, limit && i==up)) % MOD;
}
else if (id+1 <= len && check(s[id+1], pre, i)) {
tmp = (tmp + dfs(pos-1, id+1, i, all0 && i==0, limit && i==up)) % MOD;
}
else if (id>0 && check(s[id], pre, i)) {
tmp = (tmp + dfs(pos-1, id, i, all0 && i==0, limit && i==up)) % MOD;
}
}
if (!limit) f[pos][id][pre][all0] = tmp;
return tmp;
}
long long get_num(bool minus1) { // 以一贯的写法处理输入
scanf("%s", in);
int n = strlen(in);
int st = 0;
while (st < n-1 && in[st] == '0') st ++;
int pos = 0;
for (int i = n-1; i >= st; i --) {
a[pos++] = in[i] - '0';
}
if (minus1) { // 需要减1
if (pos==1 && a[0]==0) ;
else {
a[0] -= 1;
for (int i = 0; i < pos; i ++) {
if (a[i] < 0) { a[i]+=10; a[i+1]-=1; }
else break;
}
if (pos > 1 && a[pos-1]==0) pos --;
}
}
return dfs(pos-1, 0, 0, 1, true);
}
int main() {
while (~scanf("%s", s+1)) {
init();
len = strlen(s+1);
long long num_l = get_num(true);
long long num_r = get_num(false);
printf("%08lld\n", (num_r - num_l + MOD) % MOD);
}
return 0;
}

HDU3886 Final Kichiku “Lanlanshu” 题解 数位DP的更多相关文章

  1. 【HDOJ】3386 Final Kichiku “Lanlanshu”

    数位DP.需要注意的是需要特殊处理前导0,另外连续的==匹配,不要计重了,尽量贪心的匹配掉. /* 3886 */ #include <iostream> #include <sst ...

  2. POJ-2282题解&数位DP总结

    一.题意 给定一个区间[a, b](注意输入的时候可能a > b,所以,在数据输入后,要先比较a和b,如果a > b,交换a和b的值),统计这个区间里面,数位上有多少个0.多少个1.--. ...

  3. luogu2657-Windy数题解--数位DP

    题目链接 https://www.luogu.org/problemnew/show/P2657 分析 第一道数位DP题,发现有点意思 DP求\([L,R]\)区间内的XXX个数,很套路地想到前缀和, ...

  4. 洛谷P2602 [ZJOI2010]数字计数 题解 数位DP

    题目链接:https://www.luogu.com.cn/problem/P2602 题目大意: 计算区间 \([L,R]\) 范围内 \(0 \sim 9\) 各出现了多少次? 解题思路: 使用 ...

  5. 洛谷P3413 SAC#1 - 萌数 题解 数位DP

    题目链接:https://www.luogu.com.cn/problem/P3413 题目大意: 定义萌数指:满足"存在长度至少为2的回文子串"的数. 求区间 \([L,R]\) ...

  6. HDU4352 XHXJ's LIS 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 题目大意: 求区间 \([L,R]\) 范围内最长上升子序列(Longest increasin ...

  7. HDU4507 吉哥系列故事——恨7不成妻 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意: 找到区间 \([L,R]\) 范围内所有满足如下条件的数的 平方和 : 不包含'7' ...

  8. HDU3709 Balanced Number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...

  9. HDU3652 B-number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题目大意: 求区间 \([1, n]\) 范围内包含连续的数位"13"并且能 ...

随机推荐

  1. win10 uwp httpClient 登陆CSDN

    本文告诉大家如何模拟登陆csdn,这个方法可以用于模拟登陆其他网站. HttpClient 使用 Cookie 我们可以使用下面代码让 HttpClient 使用 Cookie ,有了这个才可以保存登 ...

  2. 2019-9-2-win10-uwp-应用转后台清理内存

    title author date CreateTime categories win10 uwp 应用转后台清理内存 lindexi 2019-09-02 12:57:38 +0800 2018-2 ...

  3. div盒子或者图片并排居中

    要使div总是找不到原因居中很简单,float和display都可以实现,float就不说了,这里说一下display:line-block,比如四个或者多个div盒子,明明设置好了宽度后,总有一个上 ...

  4. 一个div居于另一个div底部

    一个div如何与另一个div底部对齐,方法有很多,比如使用绝对定位 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...

  5. javascript 宽度和高度

    宽度和高度 对于编写css代码时,宽度和高度就是width和height 但是在JavaScript中,还有其他的宽度和高度,比如offsetWidth,offsetHeight,clientX,cl ...

  6. 阿里云ECS服务器活动99元一年,最高可买三年

    这几天阿里云 99一年.279三年的服务器活动如火如荼,和之前腾讯三年的服务器非常类似,非常低的价格换取非常高的价值,当然,通常情况下便宜没好货的,想要玩一下的老铁可以进阿里云去看看,阿里云270三年 ...

  7. 浅谈Transformer 及Attention网络

    1 Transformer 模型结构处理自然语言序列的模型有 rnn, cnn(textcnn),但是现在介绍一种新的模型,transformer.与RNN不同的是,Transformer直接把一句话 ...

  8. Codeforces Round #182 (Div. 1 + Div. 2)

    A. Eugeny and Array \(r-l+1\)是奇数时,和显然无法为0. 奇数的情况需要判断-1和1的个数是否大于等于长度的一半. B. Eugeny and Play List 模拟. ...

  9. Codeforces Round #194 (Div.1 + Div. 2)

    A. Candy Bags 总糖果数\(\frac{n^2(n^2+1)}{2}\),所以每人的数量为\(\frac{n}{2}(n^2+1)\) \(n\)是偶数. B. Eight Point S ...

  10. H5 存储数据sessionStorage

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...