Given a string Str, rearrange Str such that the resultant string T maximizes min (LCS(Str, T) and LCS(Str, reverse(T))).

# | User | Rating |
---|---|---|

1 | tourist | 3947 |

2 | ecnerwala | 3654 |

3 | jiangly | 3627 |

4 | jqdai0815 | 3620 |

5 | orzdevinwang | 3612 |

6 | Benq | 3586 |

7 | Radewoosh | 3582 |

8 | Geothermal | 3569 |

8 | cnnfls_csy | 3569 |

10 | ksun48 | 3474 |

# | User | Contrib. |
---|---|---|

1 | awoo | 163 |

2 | maomao90 | 160 |

3 | adamant | 156 |

4 | atcoder_official | 154 |

5 | maroonrk | 153 |

6 | -is-this-fft- | 148 |

6 | SecondThread | 148 |

8 | Petr | 147 |

9 | nor | 145 |

10 | cry | 144 |

Given a string Str, rearrange Str such that the resultant string T maximizes min (LCS(Str, T) and LCS(Str, reverse(T))).

↑

↓

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Aug/04/2024 07:51:53 (l3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

looks like min (LCS(Str, T) and LCS(Str, reverse(T))) cannot exceed longest palindromic subsequence of Str, hence T=Str should work, i may be wrong tho

Actually it can exceed that.

Let $$$str = \text{aabb}$$$, $$$T = \text{abba}$$$. Now, $$$\min(\mathrm{LCS}(\text{aabb}, \text{abba}), \mathrm{LCS}(\text{aabb}, \text{abba})) = \min(3, 3) = 3$$$.

If you choose $$$T = str$$$, you get $$$\min(\mathrm{LCS}(\text{aabb}, \text{aabb}), \mathrm{LCS}(\text{aabb}, \text{bbaa})) = \min(4, 2) = 2$$$.

yeah, i knew most probably my claim must be wrong

anyways, please tell how to solve the above problem

I think the answer is $$$\lfloor\frac{n + |P|}{2}\rfloor$$$, where $$$|P|$$$ is the maximum palindrome size.

why?

I will provide a construction as well as a proof of optimality. First, let's start with a proof of optimality. notice that $$$LCS(Str, reverse(T)) = LCS(reverse(Str), T)$$$. Now, let's say $$$s_1 = LCS(Str, T)$$$ and $$$s_2 = LCS(reverse(Str), T)$$$. Then, we must have that

As $$$T$$$ must be an intertwining of the sub-sequences $$$s_1$$$ and $$$s_2$$$ with some positions that are common to both of them, and some positions that belong to neither of them. Here $$$s_1 \cap s_2$$$ denotes the positions in $$$T$$$ that are common to both $$$s_1$$$ and $$$s_2$$$. Now, notice that $$$s_1 \cap s_2 \leq |LCS(s_1, s_2)|$$$. Therefore, we must have that

But $s_1$ is a sub-sequence of $$$Str$$$, and $$$s_2$$$ is a sub-sequence of $$$reverse(Str)$$$. Therefore,

which is the length of longest palindromic substring. Hence,

Which implies that

I will provide the construction in another comment.

Consider the longest palindromic substring of $$$Str$$$, call it $$$P = \{ p_1, p_2, ..., p_n \}$$$. Then \we must have that $$$p_i = p_{n+1-i} \forall \ 1\leq i\leq n$$$.

Now, remove this substring from $$$Str$$$, and assign the left-half substring to $$$s_1$$$ and reverse of the right-half substring to $$$s_2$$$. So, the the assignment will look like

where $a$ denotes that this position belongs to the left-half substring and $$$b$$$ denotes that it belongs to the right half sub-string. Now, we're going to construct the string $$$T$$$ as follows

Notice that for this sub-string,

and