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"并且能 ...
随机推荐
- @雅礼集训01/13 - T1@ union
目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...
- uni-app设置 video开始播放进入全屏状态
有一video标签 <video id="myVideo" :src="videoUrl"></video> 获取 video 上下文 ...
- 【tensorflow】】模型优化(一)指数衰减学习率
指数衰减学习率是先使用较大的学习率来快速得到一个较优的解,然后随着迭代的继续,逐步减小学习率,使得模型在训练后期更加稳定.在训练神经网络时,需要设置学习率(learning rate)控制参数的更新速 ...
- 神经网络入门——6and感知机
AND 感知器练习 AND 感知器的权重和偏置项是什么? 把权重(weight1, weight2)和偏置项 bias 设置成正确的值,使得 AND 可以实现上图中的运算. 在这个例子 ...
- 什么是响应式设计?响应式设计的基本原理是什么?如何兼容低版本的 IE?
响应式网站设计(Responsive Web design)的理念是:集中创建页面的图片排版大小,可以智能地根据用户行为以及 使用的设备环境(系统平台.屏幕尺寸.屏幕定向等)进行相对应的布局,无论用户 ...
- 横向tab计算滚动位置
React横向滚动计算 class Footer extends React.Component { handleClick(e) { const offset = 150; // 指定偏移量 thi ...
- poj 3572 Hanoi Tower
Hanoi Towers Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- Python--day63--单表的增删改查/GET和POST/request相关知识点回顾
- 2018-8-10-win10-uwp-使用-Geometry-resources-在-xaml
title author date CreateTime categories win10 uwp 使用 Geometry resources 在 xaml lindexi 2018-08-10 19 ...
- H3C HDLC帧格式