zoj3494+bcd code+数位dp+ac自动机

题意

1
给定区间L,R 求BCD码不包含给定字符串的个数有多少!

题解

1
非常的明显了,这道题就是ac自动机和数位dp:(以下为kuangbin的模板)

总结

1
2


code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

struct
{
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;

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,1,1);
}
char str[210];
int main()
{

// freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
int n;
while(T--)
{
ac.init();
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%s",str);
ac.insert(str);
}
ac.build();
pre_init();
memset(dp,-1,sizeof(dp));
int ans = 0;
scanf("%s",str);
int len = strlen(str);
for(int i = len -1;i >=0;i--)
{
if(str[i]>'0')
{
str[i]--;
break;
}
else str[i] = '9';
}
ans -= calc(str);
ans %=MOD;
scanf("%s",str);
ans += calc(str);
ans %=MOD;
if(ans < 0)ans += MOD;
printf("%dn",ans);
}
return 0;
}