Destopia's blog

By Destopia, history, 3 years ago, In English

Given N strings N <= 1000. The task is to choose a subsequence such that the concatenation of the chosen elements gives the maximum lexicographical string. For example N = 3, ["ab", "ac", "b"] answer = "b", N = 2 ["z", "za"] answer is "zza". How to solve this problem?

  • Vote: I like it
  • -17
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +55 Vote: I do not like it

Hi stuck, I'm Ivan!

You should solve it backwards greedily. If $$$s$$$ is the solution for the last $$$k$$$ strings, then, for the last $$$k+1$$$ it's either $$$ps$$$ or $$$s$$$, where $$$p$$$ is the $$$k+1$$$-st string from the end.

»
3 years ago, # |
  Vote: I like it +41 Vote: I do not like it

Hmm, CornHub's interface is looking rather weird today

»
3 years ago, # |
  Vote: I like it +47 Vote: I do not like it

What are you doing step-coder??

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

How is the answer for N=3, {"b", "ab", "ac"} "b", shouldn't it be "bacab". This is a greedy problem using sorting!