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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發(fā)布時(shí)間:2024/4/18 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ZOJ-3494 BCD Code (ac自动机+数位dp) 小編覺得挺不錯(cuò)的,現(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)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 看免费黄色大片 | √天堂8资源中文在线 | 蜜臀尤物一区二区三区直播 | 亚洲免费网 | 中日韩精品视频在线观看 | 色婷婷视频网 | 性色生活片 | 日韩欧美小视频 | 亚洲成av人片一区二区梦乃 | 777午夜| 午夜亚洲视频 | 性生活在线视频 | 浪潮av一区二区三区 | 男生插女生视频在线观看 | 一级影片在线观看 | wwwxx日本| 女同激情久久av久久 | 一起草最新网址 | 99精品欧美一区二区三区 | 成人在线观看免费 | 香蕉久草 | 亚洲快播| 国产精品一级片在线观看 | 欧美顶级metart裸体全部自慰 | 制服诱惑一区二区 | 91一级片 | www.亚洲黄色 | 9色视频在线观看 | 不卡日韩 | 99热网站 | 亚洲av无码成人精品区 | 成人免费毛片色戒 | 中文字幕精品三级久久久 | 国产爆乳无码一区二区麻豆 | 亚洲激情一区二区三区 | 三级全黄视频 | 国产三级日本三级在线播放 | eeuss一区 | 91丨九色丨海角社区 | 欧洲成人av | 操丝袜少妇 | 男生操女生逼逼 | 久久午夜场| 午夜嘿嘿 | 精品久久久久国产 | 在线观看视频一区二区 | 国产一区二区三区在线观看 | 亚洲一区二区三区四区电影 | 国产在线观看一区二区三区 | 日本69av| 无码人妻aⅴ一区二区三区日本 | 日本在线视频免费观看 | 成人看片黄a免费看视频 | 亚洲一区二区三区在线视频 | 日韩无遮挡 | 美国伊人网| 久久天堂视频 | 天天影视色 | 亚洲天堂最新 | 国产精品无码一区二区三区三 | 日韩av在线免费观看 | 婷婷六月天在线 | 奴色虐av一区二区三区 | 俺也去av| 国产h视频在线观看 | 国产午夜不卡 | 撸撸在线视频 | 国产伦精品一区三区精东 | 国产精品香蕉在线 | caoporn国产 | 人人做人人爽人人爱 | 白嫩初高中害羞小美女 | 疯狂做爰的爽文多肉小说王爷 | 伊人中文字幕 | 光棍影院手机版在线观看免费 | 波多野结衣亚洲一区二区 | 日本黄色的视频 | 国产精品激情偷乱一区二区∴ | 日韩不卡的av | 国产三级三级三级三级三级 | 国产一区二区三区在线视频观看 | 一区二区三区福利 | 免费在线黄色片 | 久久久无码精品亚洲国产 | 日日操影院 | 久久久久九九九 | 草草屁屁影院 | 国产一区二区三区四区三区四 | 日本韩国中文字幕 | 丁香花五月天 | 91视频在线观看网站 | 玩弄丰满少妇xxxxx性多毛 | 国产一卡二 | 欧美精品一区二区三区在线 | 麻豆影视 | 一区二区少妇 | 一区二区三区精品在线 | 一区二区三区91 | 超碰人人人人 |