I have to find a dynamic programming algorithm that solves the following problem, and I seem to be stuck:
Given a function f(w) that returns the frequency at which the word w appears in the English language (0 <= f(w) <= 1), the algorithm divideSentence(s) where s is a string of letters with no spaces must return the value of f(w) of all words in s such that divideSentence is maximised. For example: divideSentence(helloworld) should return f(hello) + f(world)
I understand that with dynamic programming, I can limit the amount of calls to f by storing the intermediate values in say a hashmap, but the way I see it, there are 2^n ways to divide the sentence and I can't see how it's possible to find the solution without trying the 2^n different solutions, because f(hell)+f(o) != f(hello). Although the teacher said it was possible to find an algorithm that finds the solution in O(n²) so clearly I'm missing something...
Can someone point me in the right direction?