ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)
题目链接:BCD Code
解析:n个病毒串。问给定区间上有多少个转换成BCD码后不包括病毒串的数。
很奇妙的题目。
。
经典的 AC自己主动机 + 数位DP 的题目。
首先使用AC自己主动机,得到bcd[i][j]表示状态i,加了数字j以后到达的状态。为-1表示不能转移
然后就是数位DP了
注意记录为0的状态
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; struct Trie{
int next[2010][2], fail[2010];
bool end[2010];
int root, L;
int newnode(){
for(int i=0; i<2; i++) next[L][i] = -1;
end[L++] = false;
return L-1;
}
void init(){
L = 0;
root = newnode();
}
void insert(char buf[]){
int len = strlen(buf);
int now = root;
for(int i=0; i<len; i++){
if(next[now][buf[i] - '0'] == -1)
next[now][buf[i] - '0'] = newnode();
now = next[now][buf[i] - '0'];
}
end[now] = true;
}
void build(){
queue<int> Q;
fail[root] = root;
for(int i=0; i<2; i++)
if(next[root][i] == -1) next[root][i] = root;
else{
fail[ next[root][i] ] = root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now = Q.front();
Q.pop();
if(end[ fail[now] ]) end[now] = true;
for(int i=0; i<2; i++)
if(next[now][i] == -1) next[now][i] = next[ fail[now] ][i];
else{
fail[ next[now][i] ] = next[ fail[now] ][i];
Q.push(next[now][i]);
}
}
}
}; Trie ac;
char buf[210]; int bcd[2010][10];
int change(int pre, int num){
if(ac.end[pre]) return -1;
int cur = pre;
for(int i=3; i>=0; i--){
if(ac.end[ac.next[cur][(num>>i)&1]]) return -1;
cur = ac.next[cur][(num>>i)&1];
}
return cur;
}
void pre_init(){
for(int i=0; i<ac.L; i++)
for(int j=0; j<10; j++)
bcd[i][j] = change(i, j);
}
const int MOD = 1000000009;
long long dp[210][2010];
int bit[210]; long long dfs(int pos, int s, bool flag, bool z){
if(pos == -1) return 1;
if(!flag && dp[pos][s] != -1) return dp[pos][s];
long long ans = 0;
if(z){
ans += dfs(pos-1, s, flag && bit[pos] == 0, true);
ans %= MOD;
}
else{
if(bcd[s][0] != -1) ans += dfs(pos-1, bcd[s][0], flag && bit[pos] == 0, false);
ans %= MOD;
}
int _end = (flag ? bit[pos] : 9);
for(int i=1; i<=_end; i++){
if(bcd[s][i] != -1){
ans += dfs(pos-1, bcd[s][i], flag && i == _end, false);
ans %= MOD;
}
}
if(!flag && !z) dp[pos][s] = ans;
return ans;
} long long calc(char s[]){
int len = strlen(s);
for(int i=0; i<len; i++) bit[i] = s[len-1 - i] - '0';
return dfs(len-1, 0, true, true);
} int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
int T;
int n;
scanf("%d", &T);
while(T--){
ac.init();
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%s", buf);
ac.insert(buf);
}
ac.build();
pre_init();
memset(dp, -1, sizeof(dp));
int ans = 0;
scanf("%s", buf);
int len = strlen(buf);
for(int i=len-1; i>=0; i--){
if(buf[i] > '0'){
buf[i] --;
break;
}
else buf[i] = '9';
}
ans -= calc(buf);
ans %= MOD;
scanf("%s", buf);
ans += calc(buf);
ans %= MOD;
if(ans < 0) ans += MOD;
printf("%d\n", ans);
}
return 0;
}
ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)的更多相关文章
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
- [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem
意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2825 Wireless Password (AC自己主动机,DP)
pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...
- POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)
题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...
- ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解
题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...
- zoj 3494:BCD Code
Description Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is rep ...
- ZOJ 3494 BCD Code (数位DP,AC自动机)
题意: 将一个整数表示成4个bit的bcd码就成了一个01串,如果该串中出现了部分病毒串,则是危险的.给出n个病毒串(n<=100,长度<21),问区间[L,R]中有几个数字是不含病毒串的 ...
- hdu 2825 Wireless Password(ac自己主动机&dp)
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- Spring整合TimerTask实现定时任务调度
一. 前言 近期在公司的项目中用到了定时任务, 本篇博文将会对TimerTask定时任务进行总结, 事实上TimerTask在实际项目中用的不多, 由于它不能在指定时间执行, 仅仅能让程序依照某一个频 ...
- 在外星人电脑上安装windows10和ubuntu16.04双系统小记
最近刚刚入手了一台Alienware Aurora R6,买这货的主要目的是为了研究Deep Learning.之所以没有买组装机的原因,主要是担心组装机的不稳定,而实验经费中的设备费也还相对充足,于 ...
- android中进程的优先级
android中进程的优先级
- 一个操作oracle的c#类 含分页
有别于以前的一个OracleHelper,这个版各有所长,MARK下. using System; using System.Data; using System.Data.OracleClient; ...
- 2019黑马JAVAEE57期基础班就业班(全套)
黑马java57期 百度网盘 2019黑马JAVAEE57期基础班就业班(全套)百度网盘 下载 Spring全家桶解决方案 - 微服务认证解决方案(JWT) - 微服务网关解决方案(Zuul) 黑马j ...
- Google浏览器vim命令
使用鼠标久了,手腕.肩膀依旧疼痛.偶尔逛知乎,看到有人推荐chrome浏览器的vimium插件(火狐浏览器是vimperator),安装了使用了几天,真不愧是浏览器神器,好用到想哭,而且非常容易上手. ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- 在 Ubuntu 15.04 上安装 Android Studio(极其简单)
sudo apt-add-repository ppa:paolorotolo/android-studio sudo apt-get update sudo apt-get install andr ...
- Mac 安装cmake小问题
今天用 brew install cmake. ==> Downloading https://homebrew.bintray.com/bottles/cmake-3.9.6.sierra.b ...
- 格式化日期字符串 FormatSettings使用
如果 你想要得到 YYYY-MM/DD 这样的字符串 你肯定说这太简单了 直接 ShowMessage(FormatDateTime('YYYY-MM/DD',now)); 运行结果 YYYY-MM ...