#P10079. [GDKOI2024 普及组] EX 中缀表达式

[GDKOI2024 普及组] EX 中缀表达式

Problem Description

Alice has recently been learning about expressions, and now he wants to write his own expression calculator.

Rules:

  • A single number: formed by concatenating several digits (at least one), and it must end with a . character (without quotes). For example, 00123. 0. 789. are all valid numbers.
  • A single operator: formed by concatenating several digits, and then ending with one character.
  • The operator character: represents the operation of this operator. This character must be one of +, *, ^, representing addition, multiplication, and exponentiation, respectively. It is specially defined that 0^0=1.
    For example, 000000789+, 123^. If the preceding numeric value is valid, then they are valid operators.
  • The digits of an operator: represent the precedence of this operator. The precedence is a positive integer in [1,n][1, n]. A larger number means higher precedence.
  • For operators with the same precedence, the problem provides a 0101 string CC of length nn to indicate whether operators of that precedence are left-associative or right-associative.
    Here, 0 means left-associative, and 1 means right-associative.
    For example, if C=111011C=111011, then the 44-th character is 0, meaning operators with precedence 44 are left-associative.
  • Left-associative: means the operator is evaluated from left to right. Example: 1.4+2.4^3.4^4. is equivalent to ((1.4+2.)4^3.)4^4., and its result is the same as ((1+2)^3)^4.
  • Right-associative: means the operator is evaluated from right to left. Example: 1.6+2.6^3.6^4. is equivalent to 1.6+(2.6^(3.6^4.)), and its result is the same as 1+(2^(3^4)).
  • Infix expression:
    1. A single number is a valid infix expression.
    2. If AA is a valid infix expression, then (A)(A) is also a valid infix expression.
    3. If AA and BB are both valid infix expressions, and cc is a valid single operator, then AcBAcB is a valid infix expression.
    4. All other cases are invalid.

Now a 0101 string CC of length nn is given to specify, for operators with the same precedence, whether they are left-associative or right-associative.

Given an infix expression, determine whether it is valid. If it is invalid, output error (without quotes). If it is valid, output the value of the expression modulo 998244353998244353.

Input Format

The first line contains a positive integer nn.

The second line contains a 0101 string CC of length nn.

The third line contains a string SS, representing an infix expression.

It is guaranteed that there are no spaces in the expression.

Output Format

If the infix expression is invalid, output error. Otherwise, output the value of the expression modulo 998244353998244353.

2
01
1.2+2.1^3.2*4.2^(5.2*6.)2+7.
243640717

Hint

Sample Explanation

243640717 = ((1+2)^(3*(4^((5*6)+7)))) mod 998244353

Constraints

Special Property 1: The ^ character will not appear.

Special Property 2: The + and * characters will not appear.

Special Property 3: The ( and ) characters will not appear.

For 8%8\% of the testdata, n=1n = 1, 1S1001 \leq |S| \leq 100, and Special Property 11 and Special Property 33 hold.

For another 8%8\% of the testdata, n=1n = 1, 1S1001 \leq |S| \leq 100, and Special Property 11 holds.

For another 20%20\% of the testdata, n=1n = 1, 1S1001 \leq |S| \leq 100, and Special Property 22 and Special Property 33 hold.

For another 24%24\% of the testdata, n=1n = 1, 1S10001 \leq |S| \leq 1000, and Special Property 22 holds.

For 100%100\% of the testdata, 1n91 \leq n \leq 9, 1S100001 \leq |S| \leq 10000.

Translated by ChatGPT 5