题意:
问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所以不影响。
1 #include <set> 2 #include <map> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 14 #define pi acos(-1.0) 15 #define eps 1e-9 16 #define fi first 17 #define se second 18 #define rtl rt<<1 19 #define rtr rt<<1|1 20 #define bug printf("****** ") 21 #define mem(a, b) memset(a,b,sizeof(a)) 22 #define name2str(x) #x 23 #define fuck(x) cout<<#x" = "<<x<<endl 24 #define sfi(a) scanf("%d", &a) 25 #define sffi(a, b) scanf("%d %d", &a, &b) 26 #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c) 27 #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d) 28 #define sfL(a) scanf("%lld", &a) 29 #define sffL(a, b) scanf("%lld %lld", &a, &b) 30 #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c) 31 #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d) 32 #define sfs(a) scanf("%s", a) 33 #define sffs(a, b) scanf("%s %s", a, b) 34 #define sfffs(a, b, c) scanf("%s %s %s", a, b, c) 35 #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d) 36 #define FIN freopen("../in.txt","r",stdin) 37 #define gcd(a, b) __gcd(a,b) 38 #define lowbit(x) x&-x 39 #define IO iOS::sync_with_stdio(false) 40 41 42 using namespace std; 43 typedef long long LL; 44 typedef unsigned long long ULL; 45 const ULL seed = 13331; 46 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; 47 const int maxn = 1e6 + 7; 48 const int maxm = 8e6 + 10; 49 const int INF = 0x3f3f3f3f; 50 const int mod = 1000000009; 51 int T, n; 52 char buf[maxn], num1[maxn], num2[maxn]; 53 int bcd[2020][12], bit[maxn]; 54 LL dp[205][2020]; 55 56 struct Aho_Corasick { 57 int next[2010][2], fail[2010], End[2010]; 58 int root, cnt; 59 60 int newnode() { 61 for (int i = 0; i < 2; i++) next[cnt][i] = -1; 62 End[cnt++] = 0; 63 return cnt - 1; 64 } 65 66 void init() { 67 cnt = 0; 68 root = newnode(); 69 } 70 71 void insert(char buf[]) { 72 int len = strlen(buf); 73 int now = root; 74 for (int i = 0; i < len; i++) { 75 if (next[now][buf[i] - '0'] == -1) next[now][buf[i] - '0'] = newnode(); 76 now = next[now][buf[i] - '0']; 77 } 78 End[now] = 1; 79 } 80 81 void build() { 82 queue<int> Q; 83 fail[root] = root; 84 for (int i = 0; i < 2; i++) 85 if (next[root][i] == -1) next[root][i] = root; 86 else { 87 fail[next[root][i]] = root; 88 Q.push(next[root][i]); 89 } 90 while (!Q.empty()) { 91 int now = Q.front(); 92 Q.pop(); 93 if (End[fail[now]]) End[now] = 1; 94 for (int i = 0; i < 2; i++) 95 if (next[now][i] == -1) next[now][i] = next[fail[now]][i]; 96 else { 97 fail[next[now][i]] = next[fail[now]][i]; 98 Q.push(next[now][i]); 99 } 100 } 101 } 102 103 int get_num(int cur, int num) { 104 if (End[cur]) return -1; 105 int now = cur; 106 for (int i = 3; i >= 0; --i) { 107 if (End[next[now][1 & (num >> i)]]) return -1; 108 now = next[now][1 & (num >> i)]; 109 } 110 return now; 111 } 112 113 void get_bcd() { 114 for (int i = 0; i < cnt; ++i) 115 for (int j = 0; j < 10; ++j) 116 bcd[i][j] = get_num(i, j); 117 } 118 119 LL dfs(int pos, int idx, int flag, int limit) { 120 if (pos == -1) return 1; 121 if (!limit && dp[pos][idx] != -1) return dp[pos][idx]; 122 int num = limit ? bit[pos] : 9; 123 LL ans = 0; 124 for (int i = 0; i <= num; ++i) { 125 if (flag && i == 0) ans = (ans + dfs(pos - 1, idx, 1, limit && i == num)) % mod; 126 else if (bcd[idx][i] != -1) ans = (ans + dfs(pos - 1, bcd[idx][i], 0, limit && i == num)) % mod; 127 } 128 if (!limit && !flag) dp[pos][idx] = ans; 129 return ans; 130 } 131 132 LL solve() { 133 get_bcd(); 134 int len1 = strlen(num1), len2 = strlen(num2); 135 for (int i = len1-1; i>=0; --i) { 136 if (num1[i] > '0') { 137 num1[i]--; 138 break; 139 } else num1[i] = '9'; 140 } 141 for (int i = 0; i < len1; ++i) bit[i] = num1[len1 - 1 - i] - '0'; 142 LL ans1 = dfs(len1 - 1, 0, 1, 1); 143 for (int i = 0; i < len2; ++i) bit[i] = num2[len2 - 1 - i] - '0'; 144 LL ans2 = dfs(len2 - 1, 0, 1, 1); 145 return (ans2 - ans1 + mod) % mod; 146 } 147 148 void debug() { 149 for (int i = 0; i < cnt; i++) { 150 printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]); 151 for (int j = 0; j < 26; j++) printf("%2d", next[i][j]); 152 printf("] "); 153 } 154 } 155 } ac; 156 157 int main() { 158 // FIN; 159 sfi(T); 160 while (T--) { 161 sfi(n); 162 ac.init(); 163 for (int i = 0; i < n; ++i) { 164 sfs(buf); 165 ac.insert(buf); 166 } 167 ac.build(); 168 sffs(num1, num2); 169 mem(dp, -1); 170 printf("%lld ", ac.solve()); 171 } 172 return 0; 173 }