Tree Generators

시간 제한2초메모리 제한2048 MB

요약
각각 무작위로 트리를 만드는 두 괄호 표현식이 주어질 때, 두 표현식 모두에서 만들어질 수 있는 트리의 수를 998244353으로 나눈 나머지로 구한다.
난이도

어려움10점 중 9점

유형
트리, 동적 계획법, 조합론, 재귀
정답자
아직 제출이 없습니다

문제

One of the problems in the International Parsing Contest caught your attention.

Two expressions are given as input, each representing a procedure to generate a single tree. The generation procedure is randomized, meaning different trees may be generated each time the procedure is performed. You are asked to count the number of trees that can possibly be generated from both of the expressions.

The syntax of an expression is as follows.

EE ::= ‘1’ | ‘(’ EE EE ‘)’

A tree is generated from an expression according to the following procedure.

  • The expression 1 generates a tree with a single vertex labeled 11.

  • For two expressions E_1E\_1 and E_2E\_2, an expression (E_1E_2E\_1E\_2) generates a tree as follows:

    • A tree T_1T\_1 is generated from E_1E\_1 with n_1n\_1 vertices, and T_2T\_2 from E_2E\_2 with n_2n\_2 vertices.
    • Then, the labels of all vertices in T_2T\_2 are incremented by n_1n\_1.
    • After that, two vertices, one from T_1T\_1 and the other from T_2T\_2, are randomly chosen. Adding an edge connecting them forms a single tree with vertices labeled 11 through (n_1+n_2)(n\_1 + n\_2), which is the tree generated by (E_1E_2E\_1E\_2).

For example, the expression (11) can generate only the leftmost tree in Figure D.1, while (1(11)) can generate the remaining two trees.

Figure D.1. Trees generated from the two expressions, (11) and (1(11))

The same tree may be generated from different expressions. The middle tree can also be generated from ((11)1).

For given two expressions of the same length, count the number of trees that can be generated from both of the expressions. Note that the trees generated from them always have the same number of vertices. Two trees are considered different if there exist two indices ii and jj such that vertices labeled ii and jj are connected by an edge in one tree but not in the other.

입력

The input consists of two lines, each containing an expression string. The two strings have the same length, between 11 and 7×1057 \times 10^5, inclusive, and follow the syntax given above.

출력

Output the number of trees that can be generated from both expressions modulo 998,244,353998\\, 244\\, 353.

힌트

For Sample Input 1, the trees that can be generated from the two expressions are shown in Figure D.2. The top six trees correspond to the first expression and the bottom four correspond to the second. Only the leftmost tree in each row can be generated from both.

Figure D.2. Illustration of Sample Input 1

예제4

  1. 예제 1

    입력
    ((1(11))1)
    ((11)(11))
    
    예상 출력
    1
    
  2. 예제 2

    입력
    (1(11))
    (1(11))
    
    예상 출력
    2
    
  3. 예제 3

    입력
    (((11)(11))((11)1))
    ((1(11))(1(1(11))))
    
    예상 출력
    3
    
  4. 예제 4

    입력
    ((11)(((1(11))1)((11)1)))
    (1(((11)((11)(11)))(11)))
    
    예상 출력
    4