KMPアルゴリズムの失敗テーブルを実装しました。
kmp s = b
where a = listArray (0,length s-1) s
b = 0:list 0 (tail s)
list _ [] = []
list n (x:xs)
| x==a!n = (n+1):list (n+1) xs
| n > 0 = list (b!!(n-1)) (x:xs)
| otherwise = 0:list 0 xs
b
はリストでb!!(n-1)
、最後の行は遅いので、スピードを上げて次のようにしました。
kmp s = b
where a = listArray (0,length s-1) s
t = listArray (0,length s-1) b
b = 0:list 0 (tail s)
list _ [] = []
list n (x:xs)
| x==a!n = (n+1):list (n+1) xs
| n > 0 = list (t!(n-1)) (x:xs)
| otherwise = 0:list 0 xs
唯一の違いは、から生成b!!
された配列であるt!
と宣言することです。t
b
同じ入力に対して、元のコードは正しい出力を持ちますが、新しいコードは単にを出力します<<loop>>
。
どうすればこの問題を解決できますか?