#P8227. Bracket Sequence Transformation

    ID: 21406 Type: Default 1000ms 256MiB

Bracket Sequence Transformation

Bracket Sequence Transformation

A bracket sequence can be abstracted as a sequence composed only of round brackets. In this problem we define two families of special bracket sequences:

  • For each non‐negative integer k, define the kth-order nested sequence Dk recursively as follows:
    • D0 is defined as ().
    • For k ≥ 1, Dk is defined as (Dk-1). In other words, Dk is obtained by adding an outer pair of parentheses to Dk-1.
    For example, ((())) is D2 because it equals ((())) = ( (()) ).
  • For each non‐negative integer k, define the kth-order flat sequence Fk recursively as follows:
    • F0 is defined as ().
    • For k ≥ 1, Fk is defined as ()Fk-1, i.e. by concatenating a pair () to the left of Fk-1.
    For example, ()()()() is F3 (note that F3 has 4 pairs of brackets).

Now, you are given two legal bracket sequences A and B of equal length n. An operation is defined as follows:

  • Choose any non-negative integer k and select a substring of the sequence which is exactly a kth-order nested sequence (Dk). Then convert this substring into the corresponding kth-order flat sequence (Fk).
  • Or, choose any non-negative integer k and select a substring which is exactly a kth-order flat sequence (Fk). Then convert this substring into the corresponding kth-order nested sequence (Dk).

Notes:

  • A legal bracket sequence is one that is balanced.
  • A substring denotes a contiguous segment of the sequence.

It can be proven that it is always possible to transform A into B using a sequence of the above operations. Your task is to compute the minimum number of operations needed to transform A into B.

Input format: Two lines containing the legal bracket sequences A and B respectively.

Output format: A single integer representing the minimum number of operations required.

inputFormat

The input consists of two lines. The first line contains the bracket sequence A and the second line contains the bracket sequence B. Both sequences have the same even length n and are legal (balanced) bracket sequences.

outputFormat

Output a single integer indicating the minimum number of operations required to transform A into B.

sample

((()))
()()()
1