-
Give a linear-time algorithm to determine whether a text T is a cyclic rotation of
another string T 0. For example, add is a cyclic rotation of dad.
n = p = # length of Tk = # matched chars
Set x = 0; k = 0;
T = T + T //copy of T
while(x < 2n-p+1) {
start from position
-
match T with P at position x;
-
Update k = #matched chars;
-
if ( k == p ) output “match at x” ;
-
Update x = x + k - π(k) ;
-
Update k = π(k) ;
}
codes here:
def get_k(T,P): k = 0 #match T with P at first position for i in range(0,len(T)): if k!=0: break if P[0] == T[i]: #oversize if ((len(P)+i)>len(T)): return(0) for j in range(0,len(P)): if(P[j]==T[i+j]): k+=1 else: x = i return(k) break break return(k)
get_k(T,P): k = 0 #match T with P at first position for i in range(0,len(T)): if k!=0: break if P[0] == T[i]: #oversize if ((len(P)+i)>len(T)): return(0) for j in range(0,len(P)): if(P[j]==T[i+j]): k+=1 else: x = i return(k) break break return(k)T = 'NDAJNFVJASKNVKASLDNVJAKSDNVKSAJNDAJNFVJASKNVKASLDNVJAKSDNVKSAJ' P = 'VJASKNVKASLDNVJAKSDNVKSAJNDAJNF' print(get_k(T,P)) #ouput:31
T = 'NDAJNFVJASKNVKASLDNVJAKSDNVKSAJNDAJNFVJASKNVKASLDNVJAKSDNVKSAJ' P = 'VJASKNVKASLDNVJAKSDNVKSAJNDAJNF' print(get_k(T,P)) #ouput:31def find_r(P,k): #if there is no prefix of P with the value of k, set r=k if(k==0): return k if(k==1): return 1 for i in range(1,k): if(P[i]==P[0]): for j in range(i,k): if P[j] != P[j-i]: break if j==k-1: r = i return r break return k
def find_r(P,k): #if there is no prefix of P with the value of k, set r=k if(k==0): return k if(k==1): return 1 for i in range(1,k): if(P[i]==P[0]): for j in range(i,k): if P[j] != P[j-i]: break if j==k-1: r = i return r break return kP = 'CCACC' find_r(P,4) #ouput:3
P = 'CCACC' find_r(P,4) #ouput:3def cyclic_rotation(L,L2): """ L = 'NDAJNFVJASKNVKASLDNVJAKSDNVKSAJ' L1 = L + L L2 ='VJASKNVKASLDNVJAKSDNVKSAJNDAJNF' """ if(len(L)!=len(L2)): print("wrong input") #find first x position for i in range(0,len(L)): if(L2[0]==L[i]): x = i break while(x <= len(L)): k = get_k(L1[x:len(L1)],L2[0:len(L)]) if k==len(L): break else: r = find_r(L2[0:len(L2)],k) if r==0: break x = x + r if(k==len(L)): print("T is a cyclic rotation of T'") else: print("T is not a cyclic rotation of T'")
def cyclic_rotation(L,L2): """ L = 'NDAJNFVJASKNVKASLDNVJAKSDNVKSAJ' L1 = L + L L2 ='VJASKNVKASLDNVJAKSDNVKSAJNDAJNF' """ if(len(L)!=len(L2)): print("wrong input") #find first x position for i in range(0,len(L)): if(L2[0]==L[i]): x = i break while(x <= len(L)): k = get_k(L1[x:len(L1)],L2[0:len(L)]) if k==len(L): break else: r = find_r(L2[0:len(L2)],k) if r==0: break x = x + r if(k==len(L)): print("T is a cyclic rotation of T'") else: print("T is not a cyclic rotation of T'") """ TRUE input T:VJASKNVKASLDNVJAKSDNVKSAJNDAJNF input P:NDAJNFVJASKNVKASLDNVJAKSDNVKSAJ """ """ WRONR input T:VJASKNVKASLDNVJAKSDNVKSAJNDAJNF input P:NDAJNFVJASKNVKASLDYVJAKSDNVKSAJ """ import datetime starttime = datetime.datetime.now() L = input("input T:") L2 = input("input P:") cyclic_rotation(L,L2) endtime = datetime.datetime.now() print (endtime - starttime).seconds
TRUE input T:VJASKNVKASLDNVJAKSDNVKSAJNDAJNF input P:NDAJNFVJASKNVKASLDNVJAKSDNVKSAJ """ """ WRONR input T:VJASKNVKASLDNVJAKSDNVKSAJNDAJNF input P:NDAJNFVJASKNVKASLDYVJAKSDNVKSAJ """ import datetime starttime = datetime.datetime.now() L = input("input T:") L2 = input("input P:") cyclic_rotation(L,L2) endtime = datetime.datetime.now() print (endtime - starttime).seconds -
-
(a)ababaa$
1: ababaa$ 8: $ 7: a$ 6: aa$ 4: abaa$ 2: ababaa$ 5: baa$ 3: babaa$
(b)
-
(a)
ababaa$baabab# [0] # [14] $baabab# [7] a [-1] $baabab# [6] a [-1] $baabab# [5] bab# [9] b [-1] # [12] a [-1] a$baabab# [3] b [-1] # [10] aa$baabab# [1] b [-1] # [13] a [-1] a [-1] $baabab# [4] bab# [8] b [-1] # [11] aa$baabab# [2]
# [14] $baabab# [7] a [-1] $baabab# [6] a [-1] $baabab# [5] bab# [9] b [-1] # [12] a [-1] a$baabab# [3] b [-1] # [10] aa$baabab# [1] b [-1] # [13] a [-1] a [-1] $baabab# [4] bab# [8] b [-1] # [11] aa$baabab# [2](b)
i. Maximum unique match s is the substring of both S1 and S2.
path(v) = s
S1 and S2 have common trie structure(v), so v is a parent with two children, one child is a leaf corresponding to a suffix of S1, and the other is a leaf corresponding to a suffix of S2 .
If there is suffix link to v, means s.t. path(u) - 1character = path(v).
There is a bigger string u which is the parent of v. Then u is the maximum unique match. If v is the maximum unique match, there is no suffix link to v.
ii. If two strings are of size M and N, then Generalized Suffix Tree construction takes O(M+N) and LCS finding is a DFS on tree which is again O(M+N).
[Algorithm&Datastructure]Suffix Trie 后缀树代码实现
最新推荐文章于 2019-10-19 16:16:00 发布