Pseudoknot
Time limit2sMemory limit512 MB
Find the largest t such that the string splits into u v z^R u^R y z with |u|>=t and |z|>=t, or report -1 if no such split exists.
- Level
Hard9 of 10
- Topics
- String, String matching, Binary search, Hash map
- Solved
- No attempts yet
Problem
An RNA strand is written here as a string of uppercase letters. When a strand folds, two reversed pairs of segments can cross each other, and that shape is called a pseudoknot. Finding the pseudoknot with the longest links is one step in the analysis of a new strand, because longer links mean a more stable structure.
A string is a pseudoknot if it can be cut into six consecutive parts
where and are the reversals of and . Here and , while and may be empty. The pair is one link and the pair is the other.
For example, ICPCINAEROKCPCIFORKOREA is a pseudoknot, with = ICPC, = IN, = AEROK, = CPCI, = FOR, and = KOREA. The string AQQQQRRRRRQQQQQ is not a pseudoknot, because no cut of it has that form.

Figure 1. A pseudoknot (a) and a string that is not a pseudoknot (b)
In ICPCAEROKCPCIKOREA both and are empty, and the string is still a pseudoknot. Only and have to be non-empty.

Figure 2. A pseudoknot whose and are empty
One string can have several pseudoknot structures. ICPCINAEROKCPCIAECIKOREA can be cut as = ICPC, = IN, = AEROK, = CPCI, = AECI, = KOREA, and it can also be cut as = IC, = PCINAEROKCPCI, = AE, = CI, = KOR, = EA. The first cut has the longer links, so it is the one to report.

Figure 3. Two pseudoknot structures for the same string
Input
Your program reads from standard input. The input is one string over the uppercase alphabet, with no whitespace and no line break inside it. The length of the string is between 1 and 200,000.
Output
Your program writes to standard output. Print one line holding the largest integer such that the input string can be cut as with , , , and . If no such exists, that is, if is not a pseudoknot, print -1 instead.