Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
JOIss2015
Search
sappleki
August 16, 2015
0
260
JOIss2015
ソースコードは
https://gist.github.com/71acf65ba6609cf54f00.gitとか
sappleki
August 16, 2015
Tweet
Share
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
360
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
920
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
110
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
440
Raft: Consensus for Rubyists
vanstee
141
7.3k
Unsuck your backbone
ammeep
671
58k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
100
Optimising Largest Contentful Paint
csswizardry
37
3.6k
Discover your Explorer Soul
emna__ayadi
2
1.1k
A designer walks into a library…
pauljervisheath
210
24k
Utilizing Notion as your number one productivity tool
mfonobong
3
220
Transcript
1 橋本早記 すべての共通接頭辞
2 • 2つのリストxsとysの最長共通接頭辞(length of the longest common prefix)の長さを llcp xs
ys とする • ex) llcp “click” “clock” = 2
3 xs a b a c a b a c
a b allcp xs 10 0 1 0 6 0 1 0 2 0 llcp “abacabacab” “abacabacab” = 10 llcp “abacabacab” “bacabacab” = 0 llcp “abacabacab” “acabacab” = 1 …...
4 • すべての共通接頭辞(all the common prefixes) の長さを表す関数allcpを考える
5 • allcp xs = map (llcp xs) (tail xs)
と定義される * tail xs --- xsの空でない末尾部分 xs tail xs
6 • allcp xs の定義を直接実行するとo(n^2) 線形時間にしたい
7 • us,vs,wsを任意の3つの文字列を用意する • llcp us vs = m llcp
vs ws = n とする • llcp us ws = min n m m/=n m + llcp (drop m us)(drop m ws) m==n * drop(k ks)-- ksのk番目までの要素を除く drop (k ks) k
8 • m<nの場合 us[m+1] != vs[m+1] vs[m+1] == ws[m+1] llcp
us ws = m • m>nの場合 us[n+1] == vs [n+1] vs[n+1] != ws[n+1] llcp us ws =n
9 • m==nの場合 drop m us , drop m vs で照合を続ける
m n ? ?us[m+1] とvs[m+1]が同じかどうかわからない
10 • 1<=i , j < n (n==length xs)として •
p = llcp xs (drop i xs) q = llcp xs (drop j xs)とおくと xs a b a c a b a c a b allcp xs 10 0 1 0 6 0 1 0 2 0 * xs[j]==q xs[i]==p xs[2]==1 xs[4]==6
11 • drop i xsはpまでの接頭辞が一致 →j<iよりjまでの接頭辞も一致 • 残りの一致部分はxsからj文字除いた文字列 drop j
xs とdrop j (drop i xs)のllcpとなる • p = j + llcp (drop j xs) (drop (i+j) xs)となる
12 • k=i+j として us = xs , vs=drop j xs
, ws =drop k xs とおく
13 • llcp xs (drop k xs)= 1. min (p-j)
q q != p-j 2. q + llcp (drop q xs) (drop (q+k) xs) q == p-j
14 1. allcp xsのk番目の要素は allcp xs [i]とallcp xs[j]だけによって決定する 2. j==p
(1<i<k && j=k-i<p)でない場合 愚直に調べる k==0,1の場合は直接計算
15 • k<i+p j=k-iとしてllcp xs (drop j xs)と llcp (drop
j xs) (drop k xs)のminを求める llcp xs (drop j xs)==llcp (drop j xs) (drop k xs)なら また愚直に求める i j p k i+p p
16 • k>=i+p 前に保存されていないので llcp xs(drop k xs)を愚直に計算する j i
p k i+p p
17 • 愚直に計算する 1回調べる度に不一致は1回 m回一致するとi+pは少なくともmずつ増える i+p<=nより一致する回数は全体で高々n回
18
19 • snoc , !! ,drop はO(n)で定数時間演算ではない データの精緻化
20 • drop Haskell配列ライブラリのData.Array を使ってllcpを書き換える • snoc HaskellのData.Sequenceライブラリを使う リストO(n)を配列に
21 • !! Data.Sequenceライブラリを使っても インデックス演算は対数時間になる
22 queueを使う *insert 新しい要素をqueueの最後に追加 *remove 先頭を取り出して残りを返す *empty-空のqueueを返す *elems-queueの要素を返す
23 実行結果
24
25 • Boyer-Mooreアルゴリズムなどの文字列照合の ための前処理であるZアルゴリズムと同じ