97
правок
Изменения
псевдокод отформатирован
=== Псевдокод ===
'''suf_arrayint[]'''suf_array(s) suf <tex>\leftarrow \= {0, 1, \dots, |.. s|\.length}</tex> '''sort''' (suf, '''compare''') '''retreturn''' suf
'''compareint''' compare (<tex>j_1</tex>j1, <tex>j_2</tex>j2) '''for''' <tex>i</tex> = 0 '''to''' <tex>|.. s|</tex> '''do'''.length '''if''' (s[(<tex>j_1j1 +i</tex>) '''mod''' <tex>|s|</tex>.length] > s[(<tex>j_2j2 +i</tex>) '''mod''' <tex>|s|</tex>.length]) '''retreturn''' 1 '''if''' (s[(<tex>j_1j1 +i</tex>) '''mod''' <tex>|s|</tex>.length] < s[(<tex>j_2j2 +i</tex>) '''mod''' <tex>|s|</tex>.length]) '''retreturn''' -1 '''retreturn''' 0
== Алгоритм, использующий хеши ==
=== Псевдокод ===
'''suf_arrayint[]'''suf_array(s) suf <tex>\leftarrow \= {0, 1, \dots, |.. s|\.length}</tex> '''sort''' (suf, '''compare''') '''retreturn''' suf
'''compareint''' compare (<tex>j_1</tex>j1, <tex>j_2</tex>j2) same <tex>\leftarrow</tex> = '''lcp'''(<tex>j_1</tex>j1, <tex>j_2</tex>j2) '''retreturn''' s[<tex>j_1</tex> j1 + same] - s[<tex>j_2</tex> j2 + same]
'''lcpint''' lcp (<tex>j_1</tex>j1, <tex>j_2</tex>j2) <tex>l</tex> <tex>\leftarrow</tex> <tex>= -1</tex> <tex>r</tex> <tex>\leftarrow</tex> <tex>|= s|.length +1</tex> '''while''' (<tex>r - l > 1</tex>) <tex>m</tex> <tex>\leftarrow</tex> <tex>= (r + l) / 2</tex> '''if''' (hash[<tex>j_1\dots j_1 j1 .. j1 +m</tex>] == hash[<tex>j_2\dots j_2 j2 .. j2 + m</tex>]) <tex>l \leftarrow = m </tex>
'''else'''
== Алгоритм, использующий префиксы циклических сдвигов ==
=== Псевдокод ===
'''suf_arrayint[]'''suf_array(s) suf <tex>\leftarrow \= {0, 1, \dots, |.. s|\.length}</tex> '''sort''' (suf, '''compare1''') <tex>c \leftarrow \= {</tex>s[0], s[1], ..., s[|s| .length - 1]<tex>\}</tex>
'''for''' <tex>l</tex> = 1 .. 2^('''toceil''' <tex>2^{\lceil \log_2 n\rceil - 1}</tex> ('''steplog2''' <tex>l \leftarrow 2l</tex> (n)) - 1) '''dostep'''l *= 2 '''sort''' (suf, '''compare2''') <tex>c'</tex>[suf[0]] <tex>\leftarrow</tex> = 0 '''for''' <tex>i</tex> = 1 '''to''' <tex>|.. s|.length -1</tex> '''do''' <tex>l_1 \leftarrow </tex> l1 = suf[<tex>i - 1</tex>] <tex>r_1 \leftarrow </tex> r1 = suf[<tex>i - 1</tex>] + <tex>l</tex> <tex>l_2 \leftarrow </tex> l2 = suf[<tex>i</tex>] <tex>r_2 \leftarrow </tex> r2 = suf[<tex>i</tex>] + <tex>l</tex> '''if''' (<tex>c</tex>[<tex>l_1</tex>l1] <tex>\neq</tex> <tex>c</tex>[<tex>l_2</tex>l2] '''or''' <tex>c</tex>[<tex>r_1</tex>r1] <tex>\neq</tex> <tex>c</tex>[<tex>r_2</tex>r2]) <tex>c'</tex>[suf[<tex>i</tex>]] = <tex>c'</tex>[suf[<tex>i - 1</tex>]] + 1
'''else'''
'''compare1int''' compare1 (<tex>j_1</tex>j1, <tex>j_2</tex>j2) '''retreturn''' s[<tex>j_1</tex>j1] - s[<tex>j_2</tex>j2]
'''compare2int''' compare2 (<tex>j_1</tex>j1, <tex>j_2</tex>j2) '''if''' (<tex>c</tex>[<tex>j_1</tex>j1] <tex>\neq</tex> <tex>c</tex>[<tex>j_2</tex>j2]) '''retreturn''' <tex>c</tex>[<tex>j_1</tex>j1] - <tex>c</tex>[<tex>j_2</tex>j2]
'''else'''
'''retreturn''' <tex>c</tex>[<tex>j_1 j1 + l</tex>] - <tex>c</tex>[<tex>j_2 j2 + l</tex>]
==Источники информации==