CC favnum (AC automation + digital DP)

Source: Internet
Author: User

Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

The question that Xiaodao gave, codechef for the first time, looks very high.

Some pattern strings are provided. The specified pattern string contains at least one pattern string.

The K specified string in the question interval.

Add the pattern string to the automatic machine, and then obtain the number of specified strings in the interval.

Just answer the second question.

In fact, it's still very watery...

Prepare to follow the suffix of Xiaodao to learn the automatic machine.

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 100000005 # define M 20005 # define n 2000 # 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 MP (a, B) make_pair (a, B) # define MEM (, b) memset (a, B, sizeof ()) # define ll long # define mod 1000000007 # define lson step <1 # define rson step <1 | 1 # define sqr (A) (a) * ()) # define key_value ch [CH [root] [1] [0] // # pragma comment (linker, "/Stack: 1024000000,1024000000") 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] [N] [2] [2]; int bit [25], length; ll DFS (INT Len, int POs, int flag, int 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; For (INT I = 0; 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;} return DFS (length, 0, 0, 1, true);} ll l, R, K; ll ans; int N; void fuck () {ll mid, TMP = slol-1); While (L <= r) {mid = (L + r)> 1; if (slove (MID)-TMP> = k) {r = mid-1; ans = mid;} else l = Mid + 1 ;}} int main () {// freopen ("1.in"," r ", stdin); While (scanf (" % LLD % d ", & L, & R, & K, & N )! = EOF) {idx = 0; MEM (DP,-1); trie * root = newnode (); For (INT I = 0; I <n; I ++) {scanf ("% s", STR); insert (root, STR, strlen (STR);} bu1__fail (Root); ans =-1; Fuck (); if (ANS =-1) printf ("No such number \ n"); else printf ("% LLD \ n", ANS) ;}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.