#P3719. [AHOI2017初中组] rexp
[AHOI2017初中组] rexp
Description
Full regular expressions are too complex. Here, we only consider regular expressions composed of (, ), |, and a. The operations follow the rules below:
- When there are parentheses, we always evaluate the part inside the parentheses first.
- When two strings (or substrings defined by parentheses) have no symbol between them, we concatenate them as a whole.
|is the OR operator, meaning you can choose either string on its two sides. If there are multiple OR operators at the same level, it can be viewed as choosing any one of the strings separated by these OR operators.
For example, (aaa)aa|aa|(a(aa)a), (aaaaa)|(aa)|aaaa, and aaaaa|aaaa|aa are equivalent. They all match all-a strings of length or .
Given a simplified regular expression, write a program to compute the maximum length of an all-a string it can match.
Input Format
One line containing a valid simplified regular expression.
Output Format
One line containing an integer, which is the maximum length of an all-a string that can be matched.
(aaa)aa|aa|(a(aa)a)
5
((a|aaa)|aa)|a
3
(a(aa|aaa)a|(a|aa))aa
7
Hint
[Constraints]
For of the testdata, the expression length does not exceed , and there are no parentheses.
For of the testdata, the expression length does not exceed .
For of the testdata, the expression length does not exceed .
For of the testdata, the expression length does not exceed .
The expression is guaranteed to be valid (i.e., the results on both sides of | and inside parentheses are non-empty strings).
Translated by ChatGPT 5
京公网安备 11011102002149号