Zoj 3494 BCD Code

Source: Internet
Author: User

The number between a and B is equal to the number of prohibited strings after being translated into bcd codes.

Question: ac automatic machine is added with a dp Based on bit. Note that, generally, when the length of a is shorter than that of B, we will add zero. As a result, 0 does not exist, when matching, the leading 0 cannot be matched. You need to make a special judgment.

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <list>
# Include <iostream>
# Include <map>
Using namespace std;
# Define inf 0x3f3f3f
# Deprecision Max 110
# Define mod 1000000009.
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Int q [25*110];
Int cnt;
Char mp [20] [10] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110 ", "0111", "1000", "1001 "};
Int up [300], down [300];
Int dp [220] [2] [2] [22*110];
Int lena, lenb;
Struct node
{
Int cnt, fail;
Int next [2];
Void init ()
{
Cnt = fail = 0;
Memset (next, 0, sizeof (next ));
}
} Tri [25*110];
Void insert (char * s)
{
Int I, p, x;
P = 0;
For (I = 0; s [I]; I ++)
{
X = s [I]-'0 ';
If (! Tri [p]. next [x])
{
Tri [++ cnt]. init ();
Tri [p]. next [x] = cnt;
}
P = tri [p]. next [x];
}
Tri [p]. cnt ++;
}
Void bfs ()
{
Int I, p, head, tail, suf;
P = 0;
Head = tail = 0;
For (I = 0; I <2; I ++)
{
If (tri [0]. next [I])
{
Q [tail ++] = tri [0]. next [I];
Tri [q [tail-1]. fail = 0;
}
}
While (head <tail)
{
P = q [head ++], suf = tri [p]. fail;
Tri [p]. cnt + = tri [suf]. cnt;
For (I = 0; I <2; I ++)
{
If (tri [p]. next [I])
{
Q [tail ++] = tri [p]. next [I];
Tri [q [tail-1]. fail = tri [suf]. next [I];
}
Else
Tri [p]. next [I] = tri [suf]. next [I];
}
}
}
Int dfs (int pos, int bg, int sl, int k)
{
If (pos = lenb) return 1;
Int ans = 0, I, j, l, r, x, tmp;
If (dp [pos] [bg] [sl] [k]! =-1)
Return dp [pos] [bg] [sl] [k];
Ans = 0;
L = bg? 0: down [pos];
R = sl? 9: up [pos];
For (I = l; I <= r; I ++)
{
Tmp = k;
If (pos> = lenb-lena | bg | I)
{
For (j = 0; j <4; j ++)
{
X = mp [I] [j]-'0 ';
Tmp = tri [tmp]. next [x];
If (tri [tmp]. cnt)
Break;
}
}
If (tri [tmp]. cnt) continue;
Ans = (ans + dfs (pos + 1, bg | (I> down [pos]), sl | (I <up [pos]), tmp) % mod;
}
Dp [pos] [bg] [sl] [k] = ans;
Return ans;
}
Char str [100], a [300], B [300], c [300];
Int main ()
{
Int I, t, n;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
Memset (dp,-1, sizeof (dp ));
Cnt = 0;
Tri [0]. init ();
While (n --)
{
Scanf ("% s", str );
Insert (str );
}
Bfs ();
Scanf ("% s", a, B );
Lena = strlen ();
Lenb = strlen (B );
If (lena <lenb)
{
Strcpy (c, );
For (I = 0; I <lenb-lena; I ++)
A [I] = '0 ';
A [lenb-lena] = 0;
Strcat (a, c );
}
For (I = 0; I <lenb; I ++)
{
Down [I] = a [I]-'0 ';
Up [I] = B [I]-'0 ';
}
Printf ("% d \ n", dfs (0, 0, 0 ));
}
}

 


Author: Wings_of_Liberty

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.