This page is still under construction.

Parts of this page are still being built. What you see may change.

RNA

Time limit1sMemory limit128 MB

Summary
Report the longest contiguous block shared by both RNA strings whose parenthesis marks balance.
Level

Hard8 of 10

Topics
Dynamic programming, String, Intervals
Solved
No attempts yet

Problem

RNA is an important molecule in the body. The structure of an RNA molecule is written as a pair (S,P)(S, P) that satisfies the following four conditions.

  1. S=s1s2…snS = s_1 s_2 \dots s_n and P=p1p2…pnP = p_1 p_2 \dots p_n for some positive integer n≤450n \le 450.
  2. Each element sis_i of SS is one of the letters A, C, G, U.
  3. Each element pip_i of PP is an asterisk, a left parenthesis, or a right parenthesis.
  4. PP is a balanced parenthesis sequence. That means that after all asterisks are ignored, each left parenthesis is matched by exactly one right parenthesis after it, and each right parenthesis is matched by exactly one left parenthesis before it.

Here are two RNA structures, A1=(S1,P1)A_1 = (S_1, P_1) on the left and A2=(S2,P2)A_2 = (S_2, P_2) on the right.

ACCCGAACUU   AAUAUCCCGAAU
((**)*(*))   *(**)(**)*()

For two RNA structures A=(S,P)A = (S, P) and A′=(S′,P′)A' = (S', P'), we call A′A' a substructure of AA if there are integers i≤ji \le j with S′=si…sjS' = s_i \dots s_j and P′=pi…pjP' = p_i \dots p_j. Since A′A' is itself an RNA structure, the parentheses of P′P' must be balanced too. For example, A3=(S3,P3)A_3 = (S_3, P_3)

CCCG
(**)

is a substructure of A1A_1, while A4=(S4,P4)A_4 = (S_4, P_4)

ACCCGAACU
((**)*(*)

is not a substructure of A1A_1, because the parentheses of P4P_4 are not balanced.

For two RNA structures XX and YY, a structure ZZ that is a substructure of both is called a common substructure of XX and YY. A longest common substructure is a common substructure of maximum length.

For the A1A_1 and A2A_2 above, the longest common substructure has length 5 and is

CCCGA
(**)*

The following is not a common substructure of A1A_1 and A2A_2, because its parentheses are not balanced.

CCCGAA
(**)*(

You are given two RNA structures of length at most 450. Report the length of a longest common substructure.

Input

The input consists of four lines. The first line is the base sequence SS of the first RNA and the second line is its parenthesis sequence PP. The third line is the base sequence S′S' of the second RNA and the fourth line is its parenthesis sequence P′P'.

Within one RNA the base sequence and the parenthesis sequence have the same length, which is between 1 and 450.

Output

Print the length of a longest common substructure on one line. Print 0 if there is no common substructure at all.

Examples2

  1. Example 1

    Input
    ACCCGAACUU
    ((**)*(*))
    AAUAUCCCGAAU
    *(**)(**)*()
    
    Expected output
    5
    
  2. Example 2

    Input
    AU
    ()
    GAUC
    *()*
    
    Expected output
    2