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 | 3803 |
2 | jiangly | 3707 |
3 | Benq | 3627 |
4 | ecnerwala | 3584 |
5 | orzdevinwang | 3573 |
6 | Geothermal | 3569 |
6 | cnnfls_csy | 3569 |
8 | Radewoosh | 3542 |
9 | jqdai0815 | 3532 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | awoo | 163 |
1 | maomao90 | 163 |
3 | adamant | 161 |
4 | maroonrk | 152 |
5 | -is-this-fft- | 151 |
6 | nor | 150 |
7 | atcoder_official | 147 |
7 | SecondThread | 147 |
9 | TheScrasse | 146 |
10 | Petr | 145 |
Given a string Str, rearrange Str such that the resultant string T maximizes min (LCS(Str, T) and LCS(Str, reverse(T))).
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