链接:
https://ac.nowcoder.com/acm/contest/33192/G
来源:牛客网
Grammy has recently been interested in regular ex
pression while focusing on cases where the alphabet consists of characters from aaa to zzz. Today she asks NIO some questions. Each question gives string AAA, asking the minimum length of expressions matching string AAA according to the matching rules and the number of all shortest expressions.
To learn detailed rules about how regular expressions match strings,
you can refer to https://en.wikipedia.org/wiki/Regular_expression.
Here we only consider characters from 'a' to 'z', '.', '?', '*', '+', '|', '(', ')'. It is assumed that the asterisk, the question mark and the plus sign have the highest priority, then concatenation and then alternation. Parentheses can be used to change the priority. For example, a(b|c)\texttt{a(b|c)}a(b|c) can match “ab” and “ac”. If there is no ambiguity then parentheses may be omitted. For example, (ab)c\texttt{(ab)c}(ab)c can be written as abc\texttt{abc}abc, and a|(b(c*))\texttt{a|(b(c*))}a|(b(c*)) can be written as a|bc*\texttt{a|bc*}a|bc*
Here are some examples of matching:
-
(or): gray|grey\texttt{gray|grey}gray|grey can match “gray” or “grey”.
-
(question mark): colou?r\texttt{colou?r}colou?r matches both “color” and “colour”.
-
(asterisk): ab*c\texttt{ab*c}ab*c matches “ac”, “abc”, “abbc”, “abbbc”, and so on.
-
(plus sign): ab+c\texttt{ab+c}ab+c matches “abc”, “abbc”, “abbbc”, and so on, but not “ac”.
-
(wildcard): a.b\texttt{a.b}a.b matches any string that contains an “a”, and then any character and then “b”; and a.*b\texttt{a.*b}a.*b matches any string that contains an “a”, and then the character “b” at some later point. More precisely, “ab” can be matched by a.*b\texttt{a.*b}a.*b but not a.b\texttt{a.b}a.b.
-
(concatenation): Let expression R=(ab|c)R=\texttt{(ab|c)}R=(ab|c) matches {ab,c}\{ ab, c\}{ab,c} and expression S=(d|ef)S=\texttt{(d|ef)}S=(d|ef) matches {d,ef}\{d, ef\}{d,ef}. Then, (RS)=((ab|c)(d|ef))(RS)=\texttt{((ab|c)(d|ef))}(RS)=((ab|c)(d|ef)) matches {abd,abef,cd,cef}\{abd, abef, cd, cef\}{abd,abef,cd,cef}
If you are not sure about whether a regular ex
pression is valid, then you can use https://regex101.com/ to check its validity. Note that the character set we use is slightly different from the website.