#P13206. [GCJ 2016 Finals] Integeregex
[GCJ 2016 Finals] Integeregex
题目描述
In this problem, a valid regular expression is one of the following. In the following descriptions, , etc. denote (not necessarily different) valid regular expressions.
- A decimal digit: that is, one of $\mathbf{0} \mathbf{1} \mathbf{2} \mathbf{3} \mathbf{4} \mathbf{5} \mathbf{6} \mathbf{7} \mathbf{8} \mathbf{9}$.
- Concatenation: .
- Disjunction: $\left(E_{1}\left|E_{2}\right| \ldots\left|E_{N}\right)\right.$, for at least two expressions. Note that the outer parentheses are required.
- Repetition: . Note that the outer parentheses are required.
For example, $7,23, (7)^{*},(45)^{*},(1|2| 3),((2)^{*} \mid 3),(1|2| 3)$, and are valid expressions. , and are not.
We say that an expression matches a string of digits if and only if at least one of the following is true:
- .
- and there exist and such that and matches .
- $E=\left(E_{1}\left|E_{2}\right| \ldots\left|E_{N}\right)\right.$ and at least one of the matches .
- and there exist for some non-negative integer such that and matches each of the . In particular, note that matches the empty string.
For example, the expression matches , and , among other strings. However, it does not match or , among other strings.
Given a valid regular expression , for how many integers between and , inclusive, does match the integer's base 10 representation (with no leading zeroes)?
输入格式
The first line of the input gives the number of test cases, . test cases follow; each consists of two lines. The first line has two positive integers and : the inclusive limits of the integer range we are interested in. The second has a string consisting only of characters in the set 0123456789()|*
, which is guaranteed to be a valid regular expression as described in the statement above.
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the number of integers in the inclusive range that the the regular expression matches.
8
1 1000
(0)*1(0)*
379009 379009
379009
1 10000
(12)*(34)*
4 5
45
1 100
((0|1))*
1 50
(01|23|45|67|23)
1 1000000000000000000
((0|1|2|3|4|5|6|7|8|9))*
1 1000
1(56|(((7|8))*9)*)
Case #1: 4
Case #2: 1
Case #3: 5
Case #4: 0
Case #5: 4
Case #6: 2
Case #7: 1000000000000000000
Case #8: 6
提示
Sample Explanation
Note that sample cases 5 through 8 would not appear in the Small dataset.
In sample case 1, the matches in range are , and .
In sample case 2, the match in range is .
In sample case 3, the matches in range are , and .
In sample case 4, there are no matches in range.
In sample case 5, the matches in range are , and .
In sample case 6, the matches in range are and .
In sample case 7, it is possible to form any number in the range.
In sample case 8, the matches in range are , and .
Limits
- .
- $1 \leqslant \mathbf{A} \leqslant \mathbf{B} \leqslant 10^{18}$.
- length of .
Small dataset (15 Pts, Test Set 1 - Visible)
- contains no | characters.
Large dataset (15 Pts, Test Set 2 - Hidden)
- No additional limits.