HDU3886 Final Kichiku “Lanlanshu” 题解 数位DP
题目链接: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) 进行求解,其中:
pos、id、pre、all0的含义和上述状态中的相同;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的更多相关文章
- 【HDOJ】3386 Final Kichiku “Lanlanshu”
数位DP.需要注意的是需要特殊处理前导0,另外连续的==匹配,不要计重了,尽量贪心的匹配掉. /* 3886 */ #include <iostream> #include <sst ...
- POJ-2282题解&数位DP总结
一.题意 给定一个区间[a, b](注意输入的时候可能a > b,所以,在数据输入后,要先比较a和b,如果a > b,交换a和b的值),统计这个区间里面,数位上有多少个0.多少个1.--. ...
- luogu2657-Windy数题解--数位DP
题目链接 https://www.luogu.org/problemnew/show/P2657 分析 第一道数位DP题,发现有点意思 DP求\([L,R]\)区间内的XXX个数,很套路地想到前缀和, ...
- 洛谷P2602 [ZJOI2010]数字计数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P2602 题目大意: 计算区间 \([L,R]\) 范围内 \(0 \sim 9\) 各出现了多少次? 解题思路: 使用 ...
- 洛谷P3413 SAC#1 - 萌数 题解 数位DP
题目链接:https://www.luogu.com.cn/problem/P3413 题目大意: 定义萌数指:满足"存在长度至少为2的回文子串"的数. 求区间 \([L,R]\) ...
- HDU4352 XHXJ's LIS 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 题目大意: 求区间 \([L,R]\) 范围内最长上升子序列(Longest increasin ...
- HDU4507 吉哥系列故事——恨7不成妻 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意: 找到区间 \([L,R]\) 范围内所有满足如下条件的数的 平方和 : 不包含'7' ...
- HDU3709 Balanced Number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...
- HDU3652 B-number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题目大意: 求区间 \([1, n]\) 范围内包含连续的数位"13"并且能 ...
随机推荐
- 初识 Knative: 跨平台的 Serverless 编排框架
Knative 是什么 Knative 是 Google 在 2018 的 Google Cloud Next 大会上发布的一款基于 Kubernetes 的 Serverless 框架.Knativ ...
- 不插字段,直接利用OracleSpatial计算
select to_char(regexp_replace(sdo_util.to_gmlgeometry(sdo_geom.sdo_difference( SDO_GEOMETRY ( 2003, ...
- 20190608笔试题のCSS-属性继承
以下的CSS属性哪些可以继承?(单选) A. font-sizeB. marginC. widthD. padding emmm,这题答案是A,看到这题我是能选对的,但又不由让我想到一 ...
- Ext.FormPanel-----FieldSet的用法
Ext.form.FieldSet的常用配置项: 1.checkboxToggle : Mixed True表示在lengend标签之前fieldset的范围内渲染一个checkbox,或者送入一个D ...
- get_magic_quotes_gpc() PHP转义的真正含义
如何正确的理解PHP转 义是一个初学者比较困扰的问题.我们今天为大家简要的讲述了PHP转义的具体含义,希望有所帮助.PHP转义一直困扰着我, 今天认真的看了一下PHP手册, 终于解决了. 在PHP中默 ...
- PHP导入导出Excel方法小结
基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...
- Linux下yum安装Redis
检验是否有yum源: [root@localhost ~]# yum install redis 显示没有软件包Redis,安装epel仓库(提供一些RHEL/CentOS默认不提供的软件包). [r ...
- Laravel5.3使用学习笔记---中间件
Laravel提供了中间件的使用.那什么是中间件呢,根据用法,我总结为,夹在“请求—>控制器—>响应—>end”中间运行的代码片段.本文将以官方英文文本为基础资料进行笔记记录. La ...
- Oracle备库宕机启动解决方案
简介 ORA-10458: standby database requires recovery ORA-01196: 文件 1 由于介质恢复会话失败而不一致 ORA-01110: 数据文件 1: ' ...
- 【codeforces 764B】Timofey and cubes
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...