日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 免费看黄色片网站 | 日本三级大全 | 国产剧情在线观看 | 三级网站视频 | 在线视频精品免费 | 韩国三级hd中文字幕叫床浴室 | 日韩欧美在线观看免费 | 麻豆www | 国产精品久久久爽爽爽麻豆色哟哟 | 4438全国成人免费 | 亚洲av无码乱码国产麻豆 | 999热 | 黄色一级片免费 | 郑艳丽三级 | 亚洲成熟少妇视频在线观看 | 久久国产精品久久久久久 | 超碰在线香蕉 | 日本性网站 | 麻豆免费观看视频 | 亚洲激情国产 | 精品毛片一区二区三区 | 熟女人妻一区二区三区免费看 | 激情全身裸吻胸 | 黄色片久久 | 波多野结衣av片 | 亚洲精品一区三区三区在线观看 | 伊人快播 | 成人免费视屏 | 成人wwwww免费观看 | 免费人成在线观看 | 狠狠爱婷婷 | 中文字幕av高清 | 中文字幕在线观看视频一区二区 | av一区在线观看 | 爱啪啪导航 | 性一交一乱一伧国产女士spa | 色播激情网 | 日本福利一区二区三区 | 久本草精品| 扒下小娇妻的内裤打屁股 | 五月天丁香久久 | 国产伦子伦对白视频 | 污片网站 | 国产精品亚洲欧美 | 用力挺进新婚白嫩少妇 | 国产精视频 | 色一涩 | 亚洲国产精品免费视频 | 亚洲另类视频 | 精品香蕉视频 | 四虎库 | av5566| 18禁免费无码无遮挡不卡网站 | 九九精品国产 | 久久久在线观看 | 久草福利 | 色在线综合| 91黄色看片| 国精产品一区一区三区 | 日本在线h | 欧美丰满美乳xxⅹ高潮www | 精品成人av一区二区在线播放 | 999国产在线| 亚洲系列 | 五月激情在线观看 | 97伊人久久 | 一区二区免费 | 1769国产精品| 成人在线一区二区 | 超碰97在线看 | av一区二区不卡 | h视频网站在线观看 | 第一区免费在线观看 | 欧美黄色一区二区三区 | 日韩欧美中出 | 日韩三级在线观看 | 操小妞| 操你啦在线视频 | 色哟哟在线 | 国产91在线视频 | 亚洲日本欧美 | 草草影院一区二区三区 | 亚洲综合免费观看高清完整版 | 日韩在线第二页 | 奇米一区二区三区 | 欧美久久久久 | 辟里啪啦国语版免费观看 | 亚洲欧美日韩专区 | 亚洲AV无码国产精品午夜字幕 | 香蕉视频网站在线观看 | 久久亚洲AV无码 | 精品自拍第一页 | 国产午夜啪啪 | 国产精品成人久久久久久久 | 韩国精品一区 | av黄在线观看| 韩国美女主播跳舞 | 69亚洲精品 | 最近中文字幕一区二区 |