BUPT 652 confusing problem (AC automation + digital DP)

Source: Internet
Author: User


Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove

Give an interval and ask how many numbers in the interval do not contain digits 0, and at least once A or B appears.

Http://acm.bupt.edu.cn/onlinejudge/newoj/showProblem/show_problem.php? Problem_id = 652

Since there are only two mode strings, A and B can actually be matched directly, although I won't

I still need to use the AC + digital DP. I did the zoj BCD code yesterday and planned to solve this problem with enthusiasm. As a result, I fell down for a long time and cried ~~~

I do not have sufficient understanding of the digital DP, and I still have a memory-based search.

I feel that the details of the digital DP are not easy to handle, for example, the leading 0, the upper and lower bounds, and so on. Now I especially feel the leading 0 problem.

Because this question cannot be 0, you still need to pay attention, but the high level can be 0, that is, there can be a leading 0 ???

I have been entangled in the memory problem. The result still uses 4 dimensions, DP [I] [J] [k] [l] to indicate that the length is I. Currently, it is at the J node of the automatic machine, k indicates whether all the high positions are 0, and l indicates whether a specified string is displayed.

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1 <30 # define M 60005 # define n 10005 # define maxn 300005 # define EPS 1e-10 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MEM (a, B) memset (a, B, sizeof ()) # define ll long # define lson step <1 # define rson step <1 | 1 # define mod 1000000009 using namespace STD; struct trie {trie * Next [10]; trie * fail; int isword, kind;}; trie * que [m], s [m]; int idx; trie * newnode () {trie * TMP = & S [idx]; MEM (TMP-> next, null); TMP-> isword = 0; TMP-> fail = NULL; TMP-> kind = idx ++; return TMP;} void insert (trie * root, Char * s, int Len) {trie * P = root; For (INT I = 0; I <Len; I ++) {If (p-> next [s [I]-'0'] = NULL) P-> next [s [I]-'0'] = newnode (); P = p-> next [s [I]-'0'];} p-> isword = 1;} void bulid_fail (trie * root) {int head = 0, tail = 0; que [tail ++] = root; root-> fail = NULL; while (Head <tail) {trie * TMP = que [head ++]; for (INT I = 0; I <10; I ++) {If (TMP-> next [I]) {If (TMP = root) TMP-> next [I]-> fail = root; else {trie * P = TMP-> fail; while (P! = NULL) {If (p-> next [I]) {TMP-> next [I]-> fail = p-> next [I]; break ;} P = p-> fail;} If (P = NULL) TMP-> next [I]-> fail = root ;} if (TMP-> next [I]-> fail-> isword) TMP-> next [I]-> isword = TMP-> next [I]-> fail-> isword; que [tail ++] = TMP-> next [I];} else if (TMP = root) TMP-> next [I] = root; else TMP-> next [I] = TMP-> fail-> next [I] ;}}char STR [100]; ll l, R; ll DP [20] [55] [2] [2]; int bit [25], length, N; ll DFS (INT Len, int POs, int flag, I NT zero, bool limit) {If (LEN <= 0) return flag = 1; if (! Limit & flag & DP [Len] [POS] [zero] [1]! =-1) return DP [Len] [POS] [zero] [1]; If (! Limit &&! Flag & DP [Len] [POS] [zero] [0]! =-1) return DP [Len] [POS] [zero] [0]; ll ans = 0; int up = limit? Bit [Len]: 9; If (zero & Len> 1) ans + = DFS (len-1, = up); For (INT I = 1; I <= up; I ++) {ans + = DFS (len-1, s [POS]. next [I]-> kind, flag | (s [POS]. next [I]-> isword), 0, limit & (I = up);} If (! Limit) {DP [Len] [POS] [zero] [flag] = ans;} return ans;} ll slove (LL num) {length = 0; while (Num) {bit [++ length] = num % 10; num/= 10;} MEM (DP,-1); Return DFS (length, 0, 0, 1, true );} int main () {int t; freopen ("1.in"," r ", stdin); freopen (" 1.out", "W", stdout); scanf ("% d ", & T); While (t --) {idx = 0; trie * root = newnode (); scanf ("% LLD", & L, & R ); scanf ("% s", STR); insert (root, STR, strlen (STR); scanf ("% s", STR); insert (root, STR, strlen (STR); bustm_fail (Root); printf ("% LLD \ n", slove (R)-slove (L-1);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.