Input: Text T and a set of n words over a finite alphabet.
We have to find the longest representation of words that when combined make up T. This can be done by merging words together.
- There could be no such representaion
- We can use the same word from the dictionary more than once
- The solution must be optimal
- There is no constraint on the length of the output
For instance, given the output:
words ={ "na", "ba", "banana", "bana", "a", "nan"}
T= "banana"
The output should be "ba""nan""a" because the number of words is 3. "bana""na" and "banana" consist of 2 and 1 word/s respectively, therefore this is not the maximum number of words, and the output should be "ba""nan""a".
I have tried to solve this with a greedy algorithm, but it does not work. So, I think that the solution is dynamic programming, but I don't know how. I tried another approach, to do this with a Trie - by searching for the current letter in the vertices of the trie.
- Search for the first letter of T in the Trie
- Check the other letters on the vertice if T is not the sole letter on the vertice
- If T is the sole letter on the vertice, check its children
- If the children do not match with the i-th letter of T, search for the ith letter in the Trie again
- If the letters on the curent vertice do not match, then there is no such representaion.
Is this an optimal, or even proper solution? If not, what is the dynamic programming solution?
Thank you in advance!