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"并且能 ...
随机推荐
- laravel 5 自定义全局函数,怎么弄呢?
在app/Helpers/(目录可以自己随便来) 下新建一个文件 functions.php 在functions.php 中加入这个方法 然后在 bootstrap/autoload.php 中添加 ...
- 2、asp.net core 部署到服务器之后外网访问不了
解决问题 把自定义端口的http://localhost:5001改成http://*:5001. 什么都没有改也不行的小伙伴试试在Program的Main方法中的.UseKestrel()后面添加. ...
- SuperSocket性能数据采集的应用程序接口的改动
性能数据采集的应用程序接口作了修改,两个虚方法已经被更改: protected virtual void UpdateServerSummary(ServerSummary serverSummary ...
- jar包运行
配置mainClass: <plugin> <groupId>org.apache.maven.plugins</gr ...
- halcon坐标转换(机器人坐标转换用)
#图像坐标r:=[431, 355, 507, 53, 507]c:=[505, 543, 316, 127, 883]#物理坐标(例如机器人坐标)r1:=[0, 2.0, -2.0, 10, -2. ...
- H3C 帧中继配置示例
- pytorch实现BiLSTM+CRF用于NER(命名实体识别)
pytorch实现BiLSTM+CRF用于NER(命名实体识别)在写这篇博客之前,我看了网上关于pytorch,BiLstm+CRF的实现,都是一个版本(对pytorch教程的翻译), 翻译得一点质量 ...
- H3C 配置Basic NAT
- [转][Linux/Ubuntu] vi/vim 使用方法讲解
vi/vim 基本使用方法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的 ...
- springmvc web.xml和application.xml配置详情(附:完整版pom.xml)
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...