RNA
Time limit1sMemory limit128 MB
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 that satisfies the following four conditions.
- and for some positive integer .
- Each element of is one of the letters
A,C,G,U. - Each element of is an asterisk, a left parenthesis, or a right parenthesis.
- 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, on the left and on the right.
ACCCGAACUU AAUAUCCCGAAU
((**)*(*)) *(**)(**)*()
For two RNA structures and , we call a substructure of if there are integers with and . Since is itself an RNA structure, the parentheses of must be balanced too. For example,
CCCG
(**)
is a substructure of , while
ACCCGAACU
((**)*(*)
is not a substructure of , because the parentheses of are not balanced.
For two RNA structures and , a structure that is a substructure of both is called a common substructure of and . A longest common substructure is a common substructure of maximum length.
For the and above, the longest common substructure has length 5 and is
CCCGA
(**)*
The following is not a common substructure of and , 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 of the first RNA and the second line is its parenthesis sequence . The third line is the base sequence of the second RNA and the fourth line is its parenthesis sequence .
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.