TADM2E 8.13

From Algorithm Wiki
Revision as of 01:19, 16 November 2015 by Anandp219 (talk | contribs) (An O(nmk) algorithm .)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

a=raw_input() # the input string m=raw_input().split(" ") # the set of tokens separated by space n=len(m) k=int(raw_input()) # the maximum length of the token l=[9999999999]*(len(a)+1) l[0]=0 for i in range(1,len(a)+1): s=a[:i] lm=max(i-k,0) while(lm<i): cost = 99999999999 if a[lm:i] in m: cost=l[lm]+1 if cost < l[i] : l[i] = cost lm=lm+1 # print "cost = ",cost

print l[-1]


Python Based implementation