Logo
(追記) (追記ここまで)

33002번 - Tree Generators 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB55393769.811%

문제

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.

$E$ ::= ‘1’ | ‘(’ $E$ $E$ ‘)

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

  • The expression 1 generates a tree with a single vertex labeled 1ドル$.
  • For two expressions $E_1$ and $E_2,ドル an expression ($E_1E_2$) generates a tree as follows:
    • A tree $T_1$ is generated from $E_1$ with $n_1$ vertices, and $T_2$ from $E_2$ with $n_2$ vertices.
    • Then, the labels of all vertices in $T_2$ are incremented by $n_1$.
    • After that, two vertices, one from $T_1$ and the other from $T_2,ドル are randomly chosen. Adding an edge connecting them forms a single tree with vertices labeled 1ドル$ through $(n_1 + n_2),ドル which is the tree generated by ($E_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 $i$ and $j$ such that vertices labeled $i$ and $j$ 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 1ドル$ and 7ドル \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,円 353$.

제한

예제 입력 1

((1(11))1)
((11)(11))

예제 출력 1

1

예제 입력 2

(1(11))
(1(11))

예제 출력 2

2

예제 입력 3

(((11)(11))((11)1))
((1(11))(1(1(11))))

예제 출력 3

3

예제 입력 4

((11)(((1(11))1)((11)1)))
(1(((11)((11)(11)))(11)))

예제 출력 4

4

힌트

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

출처

ICPC > Regionals > Asia Pacific > Japan > 2024 ICPC Asia Yokohama Regional Contest D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /