Description

Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is represented by its own binary sequence. To encode a decimal number using the common BCD encoding, each decimal digit is stored in a 4-bit nibble:

Decimal:    0     1     2     3     4     5     6     7     8     9
BCD: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001

Thus, the BCD encoding for the number 127 would be:

 0001 0010 0111

We are going to transfer all the integers from A to B, both inclusive, with BCD codes. But we find that some continuous bits, named forbidden code, may lead to errors. If the encoding of some integer contains these forbidden codes, the integer can not be transferred correctly. Now we need your help to calculate how many integers can be transferred correctly.

Input

There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.

The first line of each test case contains one integer N, the number of forbidden codes ( 0 ≤ N ≤ 100). Then N lines follow, each of which contains a 0-1 string whose length is no more than 20. The next line contains two positive integers A and B. Neither A or B contains leading zeros and 0 < A ≤ B < 10200.

Output

For each test case, output the number of integers between A and B whose codes do not contain any of the N forbidden codes in their BCD codes. For the result may be very large, you just need to output it mod 1000000009.

Sample Input

3
1
00
1 10
1
00
1 100
1
1111
1 100

Sample Output

3
9
98 还是太怂了啊……终究还是只能照着CZL的标程写出来……gg啦
先预处理出AC自动机上某个节点在它后面加上[0...9]这些数字之后会到达哪一个节点或者不能添加该数字,然后就是普通数位dp了
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int read_p,read_ca;
inline int read(){
read_p=;read_ca=getchar();
while(read_ca<''||read_ca>'') read_ca=getchar();
while(read_ca>=''&&read_ca<='') read_p=read_p*+read_ca-,read_ca=getchar();
return read_p;
}
const int LO=,MOD=;
inline void M(int &ans){
if (ans>=MOD) ans-=MOD;
}
struct tree{
int f;
bool w;
int t[LO],v[LO];
}t[];
char s[],n,m,tt;
bool us[];
int map[][],f[][],ti[][];
int num=;
queue <int> q;
inline void in(){
int m=strlen(s),p=;
for (int i=;i<m;i++){
if (!t[p].t[s[i]-]) t[p].t[s[i]-]=++num;
p=t[p].t[s[i]-];
}
t[p].w=;
}
inline void mafa(){
q.push();int k,p;t[].f=;
while (!q.empty()){
k=q.front();q.pop();
for (int i=;i<LO;i++)
if (t[k].t[i]){
p=t[k].f;
while ((!t[p].t[i])&&p) p=t[p].f;
t[t[k].t[i]].f=(k==p)?:t[p].t[i];
q.push(t[k].t[i]);
}
}
}
inline void ro(){
int i,j,p;
for (i=;i<=num;i++)
for (j=;j<LO;j++)
if (t[i].t[j]) t[i].v[j]=t[i].t[j];else{
p=t[i].f;
while ((!t[p].t[j])&&p) p=t[p].f;
t[i].v[j]=t[p].t[j];
}
}
inline void dfs(int x){
if (us[x]) return;
us[x]=;
if (t[x].w) return;
dfs(t[x].f);
t[x].w|=t[t[x].f].w;
}
inline void ju(){
int i,j,k,p;
for (i=;i<=num;i++)
if (!t[i].w)
for (j=;j<;j++){
p=i;
for (k=;k>=;k--){
p=t[p].v[((<<k)&j)>];
if (t[p].w) break;
}
if (t[p].w) map[i][j]=-;else map[i][j]=p;
}
}
inline void FI(){
for (int i=;i<=num;i++) t[i].w=t[i].f=us[i]=;
for (int i=;i<=num;i++)
for (int j=;j<LO;j++)
t[i].t[j]=t[i].v[j]=;
num=;us[]=;
}
inline void add(){
int m=strlen(s),i;
for (i=m-;i>=;i--) if (s[i]!='') break;
if (i>=){
s[i]++;for (i++;i<m;i++) s[i]='';
}else{
s[]='';for (i=;i<=m;i++) s[i]='';s[m+]=;
}
}
inline int ss(int x,int y){
if (y==) return ;
if (ti[x][y]==tt) return f[x][y];
ti[x][y]=tt;
int ans=;
for (int i=;i<;i++) if (map[x][i]!=-)
M(ans+=ss(map[x][i],y-));
return f[x][y]=ans;
}
inline int an(){
int ans=;
int i,j,p,m=strlen(s);
for (i=;i<m;i++) s[i]-=;
for (i=;i<m;i++) for (j=;j<;j++) if (map[][j]!=-) M(ans+=ss(map[][j],i-));
for(i=;i<s[];i++)if(map[][i]!=-) M(ans+=ss(map[][i],m-));
p=map[][s[]];
for (i=;i<m&&p!=-;i++){
for (j=;j<s[i];j++) if (map[p][j]!=-) M(ans+=ss(map[p][j],m-i-));
p=map[p][s[i]];
}
return ans;
}
inline void work(){
int ans;
FI();
n=read();
for (int i=;i<=n;i++) scanf("%s",s),in();
mafa();for (int i=;i<=num;i++)dfs(i);ro();ju();
scanf("%s",s);ans=an();
scanf("%s",s);add();ans=an()-ans;
printf("%d\n",(ans<?ans+MOD:ans));
}
int main(){
for (tt=read();tt;tt--) work();
}

zoj 3494:BCD Code的更多相关文章

  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. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

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

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

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

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

  5. BCD Code ZOJ - 3494 AC自动机+数位DP

    题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...

  6. ZOJ 3494 (AC自动机+高精度数位DP)

    题目链接:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...

  7. zoj3494 BCD Code(AC自动机+数位dp)

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

  8. [ZOJ3494]BCD Code

    AC自动机+数位DP. 大致题意: BCD码就是把一个数十进制下的每一位分别用4位的二进制表示. 给你一坨01串,问你在一个区间内,有多少个数的BCD码不包含任何一个字符串. 因为涉及到多个串的匹配问 ...

  9. DP ZOJ 2745 01-K Code

    题目传送门 题意:要求任意连续子序列中0和1的数量差不超过k的方案数 分析:想好状态其实不难.dp[i][j][k]表示考虑前i长度,后缀中最大的 sum(0) - sum(1) = j, sum ( ...

随机推荐

  1. 51、css初识

    前端内容就分三部分html.css.javascript(js),对一个网页来说html相当于是一个裸体的人,css相当于给这个人穿上了衣服,javascript相当于给这个人赋予动作行为,今天我们要 ...

  2. 前端构建之gulp与常用插件(转载)

    原博主:幻天芒 原文地址:http://www.cnblogs.com/humin/p/4337442.html gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建 ...

  3. TensorFlow文档翻译-01-TensorFlow入门

    版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/junyang/p/7429771.html TensorFlow入门 英文原文地址:https://w ...

  4. js变量提升与函数提升

    在es6之前,js语言并没有块级作用域,即{}形成的作用域,只有全局作用域和函数作用域,所谓的提升,即是将该变量的声明或者函数的声明提升,举个例子 console.log(global); //und ...

  5. js基础:对DOM进行操作,删除、添加元素

    <body> <div id="div1"> <p id="p1">第一段</p> <p id=" ...

  6. Webpack 2 视频教程 014 - 深入理解 Webpack 2 中的 loader

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  7. jquery中attr和prop的区别分析

    这篇文章主要介绍了jquery中attr和prop的区别分析的相关资料,需要的朋友可以参考下 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别 ...

  8. Django_调查问卷

    1.问卷的保存按钮 前端通过ajax把数据发过来后端处理数据,然后返回给前端2.对问卷做答 首先用户进行登录,验证 条件:1.只有本班的学生才能对问卷做答      2.已经参加过的不能再次访问   ...

  9. vue实现登录后跳转到之前的页面

    在开发中我们经常遇到这样的需求,需要用户直接点击一个链接进入到一个页面,用户点击后链接后会触发401拦截返回登录界面,登录后又跳转到链接的页面而不是首页,这种问题该如何去做呢? 先说一下我们需要用到的 ...

  10. python sklearn PCA源码阅读:参数n_components的设置(设为‘mle’出错的原因)

    在介绍n_components参数之前,首先贴一篇PCA参数详解的文章:http://www.cnblogs.com/akrusher/articles/6442549.html. 按照文章中对于n_ ...