zoj 3494:BCD Code

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();
}
上一篇:Deep learning with Python 学习笔记(1)


下一篇:Python学习实践------正向最大匹配中文分词