#P10079. Custom Expression Evaluator
Custom Expression Evaluator
Custom Expression Evaluator
Alice is creating her own expression calculator. The calculator evaluates infix expressions that consist of numbers, operators with custom priorities and associativities, and parentheses.
Token Definitions:
- Number Token: A sequence of one or more digits immediately followed by a dot ('.'). For example,
00123.
,0.
, and789.
are valid. - Operator Token: A sequence of one or more digits immediately followed by one of the characters:
+
,*
, or^
. The digit part represents the operator's priority.
Operator Rules:
- The operator's priority (an integer) must lie between \(1\) and \(n\) inclusive, where \(n\) is the length of a given binary string \(C\).
- The given binary string \(C\) of length \(n\) specifies the associativity for each priority level. For the operator with priority \(p\), look at the \(p\)th character of \(C\): if it is
0
then the operator is left-associative; if it is1
then it is right-associative. - Operators have precedence according to their numerical priority; a higher number means a higher precedence.
- For operators with the same priority, use the associativity to decide the evaluation order. For example:
Left-associative: 1.4+2.4^3.4^4. is evaluated as((1 + 2) ^ 3) ^ 4
.
Right-associative: 1.6+2.6^3.6^4. is evaluated as1 + (2 ^ (3 ^ 4))
. - Note: For exponentiation, the special case \(0^0 = 1\) is defined.
Expression Grammar:
- A single number token is a valid infix expression.
- If \(A\) is a valid expression, then \((A)\) is valid.
- If \(A\) and \(B\) are valid expressions and \(c\) is a valid operator token, then the concatenation AcB is a valid infix expression.
- All other forms are invalid.
Input: The first line contains a binary string \(C\) of length \(n\) (\(1 \le n \le 10^5\)) that defines the associativity of operators at each priority level. The second line contains the infix expression.
Output: If the expression is invalid, output error
(without quotes). Otherwise, compute the value of the expression modulo \(998244353\) and output the result.
inputFormat
The input consists of two lines:
- The first line is a binary string \(C\) which defines associativity for each priority. For a valid operator with priority \(p\), the \(p\)th character of \(C\) is
0
(left-associative) or1
(right-associative). - The second line is a string representing the infix expression.
outputFormat
Output a single line. If the expression is invalid, output error
. If it is valid, output the computed result modulo \(998244353\).
sample
10
123.
123