题目链接: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. iOS开发那些事-响应内存警告

    好的应用应该在系统内存警告情况下释放一些可以重新创建的资源.在iOS中我们可以在应用程序委托对象.视图控制器以及其它类中获得系统内存警告消息. 1.应用程序委托对象 在应用程序委托对象中接收内存警告消 ...

  2. 性能改善后复杂SQL

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-/ ...

  3. 记一次sublime text3更新 注册码失效问题和永久解决~

    前言: 一段时间不用sublime,打开提示我更新,不知怎么想的鬼使神差给点了~ 然后喵喵喵??? 取消 一会又出来了 受不了啦 搞事开整~ 正文: 本想直接找个注册码完事,奈何好多都用不了,想着再更 ...

  4. H3C DCC拨号配置任务

  5. Python--day46--mysql触发器

    触发器:当对某张表做:增删改操作时,可以使用触发器自定义关联行为 1,为什么需要创建mysql触发器? 比如说我往tb1表里面插入一条数据的时候,同时需要往日志表tb2中插入这条数据,这时候就需要创造 ...

  6. Python--day46--今日概要

  7. Laravel Form-builder使用

    添加formbuilder插件: Composer应用 composer require kris/laravel-form-builder 下载成功 修改配置文件 在config/app.php ‘ ...

  8. httpclient: Content-Length header already present问题

    现象:用httpclient发送http请求时,客户端返回: org.apache.http.client.ClientProtocolException at org.apache.http.imp ...

  9. [转]C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  10. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...