zoj 3494 BCD Code 数位DP + AC自动机

BCD Code

1

题意

将十进制数的每一位数位转化成一个 4 4 4 位的二进制数,并给定一些 禁止码,规定符合条件的数字的二进制表示中不能包含连续的某个禁止码。问 [ l , r ] [l,r] [l,r] 中有多少个符合条件的数字

思路

朴素的数位 D P DP DP 只涉及少量的禁止码,而这道题涉及的禁止码多达 100 100 100 个,所以要用 A C AC AC 自动机 来完成多模匹配。我们可以用当前匹配的状态在 A C AC AC 自动机的 T r i e Trie Trie 上的节点编号来表示当前的限制。

d p [ p o s ] [ n o w ] dp[pos][now] dp[pos][now] 表示 p o s pos pos 个全变化位,当前搜到了 T r i e Trie Trie 上的 n o w now now 号节点所包含的符合条件的数量。当我们枚举当前位为 0 0 0 ~ 9 9 9 时,我们要从 n o w now now 往下走 4 4 4 步,这每一步都不能走到某个禁止码的末尾,它的某个 f a i l fail fail 指针指向的点也不能是某个禁止码的末尾。由于 T r i e Trie Trie 建好后就固定了,所以我们可以预处理树上每个点往下走的路径是 0 0 0 ~ 9 9 9 10 10 10 种情况的节点编号,如果路上遇到了某个禁止码,那么就移到 − 1 -1 1 节点。

在记忆化搜索的过程中注意一下前导 0 0 0 就可以了。代码中的 Z Z Z 类型是我借鉴 j i a n g l y jiangly jiangly 的大数取模类型,可以当成一个会自动取模 l o n g l o n g long long longlong 就可以

#include<bits/stdc++.h>
#define fore(i,l,r)	for(int i=(int)(l);i<(int)(r);++i)
#define fi first
#define se second
#define endl '\n'
#define ull unsigned long long
#define ALL(v) v.begin(), v.end()
#define Debug(x, ed) std::cerr << #x << " = " << x << ed;const int INF=0x3f3f3f3e;
const long long INFLL=1e18;typedef long long ll;template<class T>
constexpr T power(T a, ll b){T res = 1;while(b){if(b&1) res = res * a;a = a * a;b >>= 1;}return res;
}constexpr ll mul(ll a,ll b,ll mod){ //快速乘,避免两个long long相乘取模溢出ll res = a * b - ll(1.L * a * b / mod) * mod;res %= mod;if(res < 0) res += mod; //误差return res;
}template<ll P>
struct MLL{ll x;constexpr MLL() = default;constexpr MLL(ll x) : x(norm(x % getMod())) {}static ll Mod;constexpr static ll getMod(){if(P > 0) return P;return Mod;}constexpr static void setMod(int _Mod){Mod = _Mod;}constexpr ll norm(ll x) const{if(x < 0){x += getMod();}if(x >= getMod()){x -= getMod();}return x;}constexpr ll val() const{return x;}explicit constexpr operator ll() const{ return x; //将结构体显示转换为ll类型: ll res = static_cast<ll>(OBJ)}constexpr MLL operator -() const{ //负号,等价于加上ModMLL res;res.x = norm(getMod() - x);return res;}constexpr MLL inv() const{assert(x != 0);return power(*this, getMod() - 2); //用费马小定理求逆}constexpr MLL& operator *= (MLL rhs) & { //& 表示“this”指针不能指向一个临时对象或const对象x = mul(x, rhs.x, getMod()); //该函数只能被一个左值调用return *this;}constexpr MLL& operator += (MLL rhs) & {x = norm(x + rhs.x);return *this;}constexpr MLL& operator -= (MLL rhs) & {x = norm(x - rhs.x);return *this;}constexpr MLL& operator /= (MLL rhs) & {return *this *= rhs.inv();}friend constexpr MLL operator * (MLL lhs, MLL rhs){MLL res = lhs;res *= rhs;return res;}friend constexpr MLL operator + (MLL lhs, MLL rhs){MLL res = lhs;res += rhs;return res;}friend constexpr MLL operator - (MLL lhs, MLL rhs){MLL res = lhs;res -= rhs;return res;}friend constexpr MLL operator / (MLL lhs, MLL rhs){MLL res = lhs;res /= rhs;return res;}friend constexpr std::istream& operator >> (std::istream& is, MLL& a){ll v;is >> v;a = MLL(v);return is;}friend constexpr std::ostream& operator << (std::ostream& os, MLL& a){return os << a.val();}friend constexpr bool operator == (MLL lhs, MLL rhs){return lhs.val() == rhs.val();}friend constexpr bool operator != (MLL lhs, MLL rhs){return lhs.val() != rhs.val();}
};const ll mod = 1000000009;
using Z = MLL<mod>;char code[220]; //错误码
int cnt; //AC自动机节点数量struct node{int son[2];bool end; //code结尾标记int fail;
}tree[2500];int nxt[2500][10]; //Trie上的点往后移动后的节点编号Z dp[220][2500];
int num[220];void insert(char* s){int now = 0;int n = strlen(s);fore(i, 0, n){int ch = s[i] - '0';if(!tree[now].son[ch])tree[now].son[ch] = ++cnt;now = tree[now].son[ch];}tree[now].end = true;
}void getFail(){std::queue<int> q;fore(i, 0, 2)if(tree[0].son[i])q.push(tree[0].son[i]);while(!q.empty()){int now = q.front();q.pop();tree[now].end |= tree[tree[now].fail].end; //code结尾标记向下传递fore(i, 0, 2){if(tree[now].son[i]){tree[tree[now].son[i]].fail = tree[tree[now].fail].son[i];q.push(tree[now].son[i]);}else tree[now].son[i] = tree[tree[now].fail].son[i];}}
}int judge(int now, int d){ //d是十进制数,将其转成二进制后在Trie上移动for(int i = 3; i >= 0; --i){int ch = ((d >> i) & 1);if(tree[tree[now].son[ch]].end) return -1; //下一个位置是一个禁止码now = tree[now].son[ch]; //往下走}return now;
}Z dfs(int pos, int now, bool lead, bool limit){if(!pos) return 1;if(!lead && !limit && dp[pos][now].x != -1) return dp[pos][now];Z res = 0;int up = (limit ? num[pos] : 9);fore(i, 0, up + 1){if(lead && !i) res += dfs(pos - 1, now, true, limit && i == up);else if(nxt[now][i] != -1) res += dfs(pos - 1, nxt[now][i], false, limit && i == up);}if(!lead && !limit) dp[pos][now] = res;return res;
}Z solve(std::string& s){int len = 0;for(auto it = s.rbegin(); it != s.rend(); ++it){num[++len] = *it - '0';}return dfs(len, 0, true, true);
}int main(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);int t;std::cin >> t;while(t--){fore(i, 0, 220)fore(j, 0, 2200)dp[i][j].x = -1;int n;std::cin >> n;while(n--){std::cin >> code; //读入错误码insert(code);}getFail();/* 预处理Trie上每个节点往后走的情况的节点编号,以及是否会走到某个code */fore(i, 0, cnt + 1)fore(j, 0, 10)nxt[i][j] = judge(i, j);std::string l, r;std::cin >> l >> r;/* l - 1 */for(int i = l.size() - 1; i >= 0; --i)if(l[i] == '0')l[i] = '9';else{--l[i];break;}Z ans = solve(r) - solve(l);std::cout << ans << endl;/* 重置 Trie */fore(i, 0, cnt + 1){tree[i].fail = tree[i].end = 0;fore(j, 0, 2) tree[i].son[j] = 0;}cnt = 0;}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/417609.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Rancher部署k8s集群测试安装nginx(节点重新初始化方法,亲测)

目录 一、安装前准备工作计算机升级linux内核时间同步Hostname设置hosts设置关闭防火墙&#xff0c;selinux关闭swap安装docker 二、安装rancher部署rancher 三、安装k8s安装k8s集群易错点&#xff0c;重新初始化 四、安装kutectl五、测试安装nginx工作负载 一、安装前准备工作…

羊驼系列大模型LLaMa、Alpaca、Vicuna

羊驼系列大模型&#xff1a;大模型的安卓系统 GPT系列&#xff1a;类比ios系统&#xff0c;不开源 LLaMa让大模型平民化 LLaMa优势 用到的数据&#xff1a;大部分英语、西班牙语&#xff0c;少中文 模型下载地址 https://huggingface.co/meta-llama Alpaca模型 Alpaca是斯…

el-table样式错乱解决方案

bug&#xff1a; 图片的椭圆框住的地方&#xff0c;在页面放大缩小之后就对不齐了。 原因&#xff1a; 主要原因是当你对页面放大缩小的时候&#xff0c;页面进行了重构&#xff0c;页面的宽高及样式进行了变化&#xff0c;但是在这个更新的过程中&#xff0c;table的反应并没…

高功率PCB设计中的EMC挑战与解决策略

在高功率PCB设计中&#xff0c;电磁兼容性&#xff08;EMC&#xff09;是一个关键问题&#xff0c;它涉及到保证电子设备在各种环境下正常运作&#xff0c;不受电磁干扰&#xff08;EMI&#xff09;的影响&#xff0c;同时也不对其他设备产生干扰。本文将从一个全面的视角探讨高…

C#,因数分解(质因子分解)Pollard‘s Rho算法的源代码

因数分解&#xff08;也称为质因子分解&#xff09;&#xff1a;将一个大整数分解它的质因子之乘积的算法。 Pollard Rho算法的基本思路&#xff1a;先判断当前数是否是素数&#xff08;质数&#xff09;&#xff0c;如果是&#xff0c;则直接返回。如果不是&#xff0c;继续找…

RabbitMQ入门篇【图文并茂,超级详细】

&#x1f973;&#x1f973;Welcome 的Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于Docker的相关操作吧 目录 &#x1f973;&#x1f973;Welcome 的Huihuis Code World ! !&#x1f973;&#x1f973; 前言 1.什么是MQ 2.理解MQ 3.生活…

什么是安全SCDN,有什么作用?

前两天有个站长被朋友推荐联系到了德迅云安全&#xff0c;想要对自己网站做一些安全防护&#xff0c;聊天中问及到了安全SCDN是什么意思&#xff0c;有哪些作用&#xff1f;那么德迅云安全今天就来简单讲述一下安全SCDN&#xff0c;来了解下什么是安全SCDN&#xff0c;以及它有…

JVM:Java类加载机制

Java类加载机制的全过程&#xff1a; 加载、验证、准备、初始化和卸载这五个阶段的顺序是确定的&#xff0c;类型的加载过程必须按照这种顺序按部就班地开始&#xff0c;而解析阶段则不一定&#xff1a;它在某些情况下可以在初始化阶段之后再开始&#xff0c; 这是为了支持Java…

10 个优化技巧,减少 Docker 镜像大小

什么是 docker? Docker 是一种容器引擎,可以在容器内运行一段代码。Docker 镜像是在任何地方运行您的应用程序而无需担心应用程序依赖性的方式。 要构建镜像,docker 使用一个名为 Dockerfile 的文件。Dockerfile 是一个包含许多指令(RUN、COPY、EXPOSE 等)的文件。成功执…

索引的概述和性能分析

索引index&#xff0c;是一种有序的数据结构&#xff0c;可以高效的获取数据&#xff0c;在数据库中维护着满足查找特定算法的数据结构&#xff0c;就是索引 无索引的情况&#xff0c;查询数据时会全表扫描&#xff0c;效率极低 索引结构 &#xff08;1&#xff09;二叉树&…

防火墙部署安全区域

目录 为什么需要安全区域在防火墙上如何来区分不同的网络将接口划分到安全区域安全区域、受信任程度与安全级别安全域间、安全策略与报文流动的方向 安全区域配置案例 为什么需要安全区域 防火墙主要部署在网络边界起到隔离的作用 在防火墙上如何来区分不同的网络 防火墙通过安…

力扣36. 有效的数独

模拟 思路&#xff1a; 使用三个哈希表来存储数字个数 row[r][val] 用于存储第 r 行 val 1 的个数&#xff1b;column[c][val] 用于存储第 c 列 val 1 的个数&#xff1b; subboxes[i][j][val] 用于存储第 i 行、第 j 列个小九宫格 val 1 的个数&#xff0c;其中&#xff1…