Difference between revisions of "TADM2E 8.13"

From Algorithm Wiki
Jump to: navigation, search
(An O(nmk) algorithm .)
 
m
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
a=raw_input()                        # the input string
+
 
m=raw_input().split(" ")            # the set of tokens separated by space
+
    a=raw_input()                        # the input string
n=len(m)
+
    m=raw_input().split(" ")            # the set of tokens separated by space
k=int(raw_input())                  # the maximum length of the token
+
    n=len(m)  
l=[9999999999]*(len(a)+1)     
+
    k=int(raw_input())                  # the maximum length of the token
l[0]=0
+
    l=[float("inf")]*(len(a)+1)     
for i in range(1,len(a)+1):
+
    l[0]=0
s=a[:i]
+
    for i in range(1,len(a)+1):
lm=max(i-k,0)
+
        lm = 0
while(lm<i):
+
        while lm<i:
cost = 99999999999
+
            cost = float("inf")
if a[lm:i] in m:
+
            if a[lm:i] in m:
cost=l[lm]+1
+
                cost=l[lm]+1
if cost < l[i] :
+
            if cost < l[I] :
l[i] = cost
+
                l[i] = cost
lm=lm+1
+
            lm=lm+1
# print "cost = ",cost
+
    print(l[-1])
 
print l[-1]
 
  
  
 
Python Based implementation
 
Python Based implementation

Latest revision as of 20:20, 25 April 2020

   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=[float("inf")]*(len(a)+1)     
   l[0]=0
   for i in range(1,len(a)+1):
       lm = 0
       while lm<i:
           cost = float("inf")
           if a[lm:i] in m:
               cost=l[lm]+1
           if cost < l[I] :
               l[i] = cost
           lm=lm+1
   print(l[-1])


Python Based implementation