n1e2m3's blog

By n1e2m3, history, 4 weeks ago, In English,

given a string S. you have to find maximum product of length of two non overlapping palindromic sequence.
here, non overlapping means only indexes are non overlap.

for example,

1)
s = “abcab”
ans = 3*2 = 6
two palindromic sequence are “aca” and “bb”

2 )
s = nriatdoiarn
ans = 5*5 = 25
two palindromic sequences are “nitin” and ( “radar” or “raoar”)

3 )
s = “abcdef”
ans = 1*1 = 1

please share your solution.

Easy Version link : https://stackoverflow.com/questions/53663721/find-the-maximum-product-of-two-non-overlapping-palindromic-subsequences

 
 
 
 
  • Vote: I like it
  • +11
  • Vote: I do not like it

»
4 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

[Deleted]

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Could you elaborate more about how to find the length of the longest palindromic subsequence in O(n*logn)?

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Please, someone answer it. This appears to be a very good question to me.

»
4 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

stackexchange code is wrong

»
4 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Wrong idea

»
4 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

What about the stack exchange answer. It looks correct to me, and is $$$O(n^{2})$$$. Please point out if something is wrong.

So another question is, for $$$A$$$ and $$$B$$$ to be non overlapping subsequences of given string $$$S$$$, what is the exact criteria?

Since $$$A$$$ = $$$S[i_1]$$$,$$$S[i_2]$$$, ... , $$$S[i_{n1}]$$$, and $$$B$$$ = $$$S[j_1]$$$,$$$S[j_2]$$$, ... , $$$S[j_{n2}]$$$ so which of the following do we want: ( assuming $$$i_1$$$ < $$$j_1$$$ )

$$$1)$$$ $$$i_{n1} \lt j_1$$$ ( basically for some string, "abcdefg" if I take $$$A$$$ = "ace" then can I only take letters after 'e' for $$$B$$$? )

OR

$$$2)$$$ $$$i_x != j_y$$$ for each $$$x$$$ in $$$range(1,n1)$$$ ,$$$y$$$ in $$$range(1,n2)$$$. ( no two characters of the same index )

I believe the stack exchange answer is definitely valid, in case of definition $$$1)$$$ of non overlapping subsequences.

UPD: I guess the example $$$2$$$ given in the post, makes it clear that the $$$2)$$$ definition is the one to be used.

  • »
    »
    4 weeks ago, # ^ |
    Rev. 3   Vote: I like it +1 Vote: I do not like it

    accbcac its ans is 12 its showing 6, author's concept might be correct,but implementation is wrong. link

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How it's answer is 12? It should be 6.

    • »
      »
      »
      4 weeks ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Interesting example. It looks like longest palindromic subsequence dp will fail to give us required answer. For $$$accbcac$$$, lps is length $$$5$$$ ( either $$$accca$$$ or $$$ccbcc$$$ or $$$acbca$$$ ), but we need $$$aba$$$ and $$$cccc$$$ for maximum product.

      • »
        »
        »
        »
        4 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        i know one greedy approach,we know that for sure for palindrome pair both characters should be at maximum distance.but we should insert them in two sets in such way that they should for approximate equal length.

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    We can also do like max(dp[1][j]*dp[j+1][n-1])

»
4 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Wrong idea, misread the problem statement.

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yeah, you need to consider subsequences, not substrings. And even among subsequences, it is not necessary that subsequences will not interleave ( but definitely not intersect ).