日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ-3494 BCD Code (ac自动机+数位dp)

發(fā)布時(shí)間:2024/4/18 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ-3494 BCD Code (ac自动机+数位dp) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

Problem 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

31001 101001 100111111 100

Sample Output

3998

AC

  • 涉及到多個(gè)字符串的匹配問題,AC自動機(jī)
  • 初始化AC自動機(jī),這里有一個(gè)處理,就是如果當(dāng)前節(jié)點(diǎn)的fail指向終點(diǎn)的話,那么這個(gè)節(jié)點(diǎn)也標(biāo)記為終點(diǎn)(end[pos]++),一個(gè)長的字符串可能包含一個(gè)短的字符串
  • 預(yù)處理一個(gè)BCD[i][j],表示在AC自動機(jī)的第i個(gè)位置后添加數(shù)字j,如果不能添加就賦值為-1,如果可以賦值為新的位置
  • 數(shù)位dp
#include <iostream>#include <stdio.h>#include <map>#include <vector>#include <set>#include <queue>#include <stack>#include <cstring>#include <cmath>#include <iomanip>#include <algorithm>#define N 2001#define mem(a, b) memset(a, b, sizeof(a))typedef long long LL;using namespace std;struct Trie{int next[N][2], fail[N], end[N];int L, root;int newnode() {for (int i = 0; i < 2; ++i)next[L][i] = -1;end[L++] = 0;return L-1;}void init() {L = 0;root = newnode();}void insert(char s[]) {int len = strlen(s);int now = root;for (int i = 0; i < len; ++i) {if (next[now][s[i]-'0'] == -1)next[now][s[i]-'0'] = newnode();now = next[now][s[i]-'0'];}end[now]++;}void build() {queue<int> que;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;que.push(next[root][i]);}}while (!que.empty()) {int now = que.front();que.pop();if (end[fail[now]]) end[now]++; // 至關(guān)重要的一句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];que.push(next[now][i]);}}}}}ac;char s1[201], s2[201];int bcd[2001][10];int dp[201][2001];int dig[201];const int mod = 1e9 + 9;int find(int pos, int num) {if (ac.end[pos]) return -1;for (int i = 3; i >= 0; --i){int t = ac.next[pos][(num >> i) & 1];if (ac.end[t]) return -1;pos = t;}return pos;}void init() {for (int i = 0; i < ac.L; ++i) {for (int j = 0; j < 10; ++j) {bcd[i][j] = find(i, j);}}}int dfs(int len, int sta, bool top, bool zero) {if (sta == -1) return 0;if (!len) return 1;if (!top && dp[len][sta] != -1 && !zero) return dp[len][sta];int t = top ? dig[len] : 9;int ans = 0;for (int i = 0; i <= t; ++i) {if (i == 0 && zero)ans += dfs(len-1, sta, top && i==t, zero && !i);elseans += dfs(len-1, bcd[sta][i], top && i==t, zero && !i);ans %= mod;}if (!top && !zero)dp[len][sta] = ans;return ans;}int solve(char s[]) {int len = strlen(s);for (int i = 1; i <= len; ++i) {dig[i] = s[len-i] - '0';}return dfs(len, 0, 1, 1);}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endifint t;scanf("%d", &t);while (t--) {int n;scanf("%d", &n);ac.init();for (int i = 0; i < n; ++i) {scanf("%s", s1);ac.insert(s1);}ac.build();init();mem(dp, -1);scanf("%s%s", s1, s2);int len = strlen(s1);for (int i = len - 1; i >= 0; --i) {if (s1[i] == '0') s1[i] = '9';else {s1[i]--;break;}}int ans = solve(s2) - solve(s1);ans = (ans + mod) % mod;printf("%d\n", ans);}return 0;}

總結(jié)

以上是生活随笔為你收集整理的ZOJ-3494 BCD Code (ac自动机+数位dp)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。