题目链接: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)的更多相关文章

  1. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...

  2. [AC自己主动机+可能性dp] hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

  3. POJ 3691 &amp; 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: ...

  4. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  5. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  6. ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解

    题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...

  7. zoj 3494:BCD Code

    Description Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is rep ...

  8. ZOJ 3494 BCD Code (数位DP,AC自动机)

    题意: 将一个整数表示成4个bit的bcd码就成了一个01串,如果该串中出现了部分病毒串,则是危险的.给出n个病毒串(n<=100,长度<21),问区间[L,R]中有几个数字是不含病毒串的 ...

  9. hdu 2825 Wireless Password(ac自己主动机&amp;dp)

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. This Activity already has an action bar supplied by the window decor

    问题描写叙述:继承自AppCompatActivity,使用Toolbar替代ActionBar的时候.出现错误 错误信息: 2.Caused by: java.lang.IllegalStateEx ...

  2. NSAttributedString编程

    - (void)viewDidLoad {     [super viewDidLoad];          NSMutableAttributedString *attributedString ...

  3. ASP.NET Core-组件-后台任务:Hangfire

    ylbtech-ASP.NET Core-组件-后台任务:Hangfire Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的 ...

  4. 关于懒加载中的self.和_

    ---恢复内容开始--- 在开发中,经常会用到懒加载,最常用的如加载一个数组 如图,在这个懒加载数组中有的地方用到了_array有的地方用到了self.array 原因是_array是直接访问,而se ...

  5. 【转】SQL语句删除和添加外键、主键

    --删除外键 语法:alter table 表名 drop constraint 外键约束名 如: alter table Stu_PkFk_Sc drop constraint FK_s alter ...

  6. windows中安装redis的phpredis扩展

    1. 下载php的redis扩展 打开网址 http://pecl.php.net/ (php的扩展库官网),搜索redis,进入地址:http://pecl.php.net/package/redi ...

  7. sql server 清理数据库日志

    USE [master] GO ALTER DATABASE [数据库名] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [数据库名] SET ...

  8. CLR寄宿和应用程序域

    Win实际上将CLR作为一个COM服务器实现在一个DLL内,即为CLR定义了标准的COM接口,并为该接口和COM服务器分配一GUID,安装FrameWork时表示CLR的COM服务器被注册到注册表内. ...

  9. Core Java(四)

    四.数组 数组就是主函数(main方法)中的参数:public static void main(String[] args){    }数组是指一组数据的集合,数组中的每个数据称为元素.在Java中 ...

  10. jmeter的认识——线程组的认识

    名称:可以给线程组设置一个个性化的命名 注释:可以对线程组添加备注以标记 在取样器错误后要执行的动作:就是在错误之后要如何执行,可选继续执行后续的.停止执行等. 线程数:就是需要设置多少线程执行测试. ...