Monday, 15 July 2013

algorithm - Data structure choice for dynamic programming -



algorithm - Data structure choice for dynamic programming -

in order implement dp algorithm detecting whether word breakable sub-words of length 'n' 1, should selection of info construction caching dp results. example: the word 'pita' can broken 'pit', 'it' & 'i' valid dictionary words well. dp algorithm should observe such words.

i can imagine need start words of length 1 , build substrings of increasing lengths not able arrive @ info construction can efficiently store subproblem's results

if work on word sequential don't need track previous word don't reach them again, can utilize cache cache lookup sub word in dictionary. if understand problem, think next java code may help you:

string term = "pita"; set<string> dictionary = new hashset<string>(); boolean ans = true; for(int = 0; < term.length() && ans == true; i++) { for(int j = + 1; j < term.length(); j++) { string subterm = term.substring(i, j); if(!dictionary.contains(subterm)){ ans = false; break; } } } system.out.println("ans [" = ans + "]");

for dictionary can utilize hash table , back upwards take o(1) check if sub word exist or not. if cache checked sub word take same o(1)

i think problem fitted in sorting , searching technique not in dp because did not utilize previous reply produce current answer.

algorithm data-structures dynamic-programming

No comments:

Post a Comment