题意:

问A到B之间的所有整数,转换成BCD Code后,

有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000.

BCD码这个在数字电路课上讲了,题干也讲的很详细。

数位DP的实现是通过0~9 ,并不是通过BCD码

所有我们需要先把字串放入AC自动机,建立一个BCD数组

因为BCD码是一个4位二进制数,但是tire图上全是0,1,

所以对于一个数字,我们的要在转移4次,

如果中间出现了病毒串就return -1 表示不能转移,

BCD【i】【j】表示在AC自动机 i 这个节点转移到数字 j 对应的在AC自动机上的节点标号。

然后就是简单的数位DP了,然而我写搓了,由于没有前导0所以前导0要处理掉。

但是你不转移的时候,不能 bcd[idx][i] != -1 就直接continue ,

因为有0的情况,i==0 但是(bcd[idx][i] == -1) 但是这个0是前导0所以不影响。

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = ;
int T, n;
char buf[maxn], num1[maxn], num2[maxn];
int bcd[][], bit[maxn];
LL dp[][]; struct Aho_Corasick {
int next[][], fail[], End[];
int root, cnt; int newnode() {
for (int i = ; i < ; i++) next[cnt][i] = -;
End[cnt++] = ;
return cnt - ;
} void init() {
cnt = ;
root = newnode();
} void insert(char buf[]) {
int len = strlen(buf);
int now = root;
for (int i = ; i < len; i++) {
if (next[now][buf[i] - ''] == -) next[now][buf[i] - ''] = newnode();
now = next[now][buf[i] - ''];
}
End[now] = ;
} void build() {
queue<int> Q;
fail[root] = root;
for (int i = ; i < ; i++)
if (next[root][i] == -) 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] = ;
for (int i = ; i < ; i++)
if (next[now][i] == -) next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} int get_num(int cur, int num) {
if (End[cur]) return -;
int now = cur;
for (int i = ; i >= ; --i) {
if (End[next[now][ & (num >> i)]]) return -;
now = next[now][ & (num >> i)];
}
return now;
} void get_bcd() {
for (int i = ; i < cnt; ++i)
for (int j = ; j < ; ++j)
bcd[i][j] = get_num(i, j);
} LL dfs(int pos, int idx, int flag, int limit) {
if (pos == -) return ;
if (!limit && dp[pos][idx] != -) return dp[pos][idx];
int num = limit ? bit[pos] : ;
LL ans = ;
for (int i = ; i <= num; ++i) {
if (flag && i == ) ans = (ans + dfs(pos - , idx, , limit && i == num)) % mod;
else if (bcd[idx][i] != -) ans = (ans + dfs(pos - , bcd[idx][i], , limit && i == num)) % mod;
}
if (!limit && !flag) dp[pos][idx] = ans;
return ans;
} LL solve() {
get_bcd();
int len1 = strlen(num1), len2 = strlen(num2);
for (int i = len1-; i>=; --i) {
if (num1[i] > '') {
num1[i]--;
break;
} else num1[i] = '';
}
for (int i = ; i < len1; ++i) bit[i] = num1[len1 - - i] - '';
LL ans1 = dfs(len1 - , , , );
for (int i = ; i < len2; ++i) bit[i] = num2[len2 - - i] - '';
LL ans2 = dfs(len2 - , , , );
return (ans2 - ans1 + mod) % mod;
} void debug() {
for (int i = ; i < cnt; i++) {
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
for (int j = ; j < ; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
} ac; int main() {
// FIN;
sfi(T);
while (T--) {
sfi(n);
ac.init();
for (int i = ; i < n; ++i) {
sfs(buf);
ac.insert(buf);
}
ac.build();
sffs(num1, num2);
mem(dp, -);
printf("%lld\n", ac.solve());
}
return ;
}
05-11 22:53