BoyerMoore.class RabinKarp.class

4. BoyerMoore.class

package com.tongam.uupa; import java.util.HashMap; import java.util.Map; public class BoyerMoore { private String pattern ; private String text ; private int foundedAt = ; private long duration = ; public BoyerMooreString pattern, String text { this . pattern = pattern; this . text = text; } public int getFoundedAt { return foundedAt ; } public long getDuration { return duration ; } private static void preBmBc char [] x, int m, int bmBc[] { int i; for i = ; i bmBc. length ; ++i bmBc[i] = m; for i = ; i m - 1 ; ++i bmBc[x[i]] = m - i - 1 ; } private static int [] suffixes char [] x, int m { int f= , g, i; int suff[] = new int [m]; suff[m - 1 ] = m; g = m - 1 ; for i = m - 2 ; i = ; --i { if i g suff[i + m - 1 - f] i - g suff[i] = suff[i + m - 1 - f]; else { if i g g = i; f = i; while g = x[g] == x[g + m - 1 - f] --g; suff[i] = f - g; A-3 Universitas Sumatera Utara } } return suff; } private static void preBmGs char [] x, int m { int i, j; int suff[] = suffixesx, m; for i = ; i m; ++i bmGs [i] = m; j = ; for i = m - 1 ; i = ; --i if suff[i] == i + 1 for ; j m - 1 - i; ++j if bmGs [j] == m bmGs [j] = m - 1 - i; for i = ; i = m - 2 ; ++i bmGs [m - 1 - suff[i]] = m - 1 - i; } static int [] bmGs ; static int [] bmBc ; public boolean check2 { long startMillis = System.currentTimeMillis; long endMillis = ; boolean result = false ; int n = text .length; int m = pattern .length; char [] y = text .toCharArray; char [] x = pattern .toCharArray; int i, j; bmGs = new int [m]; bmBc = new int [ 256 ]; Preprocessing preBmGsx, m; preBmBcx, m, bmBc ; Searching j = ; while j = n - m { for i = m - 1 ; i = x[i] == y[i + j]; --i; if i { j += bmGs [ ]; this . foundedAt = j-m; A-4 Universitas Sumatera Utara endMillis = System.currentTimeMillis; this . duration = endMillis - startMillis; result = true ; } else j += java.lang.Math.max bmGs [i], bmBc [y[i + j]] - m + 1 + i; } return result; } }

5. RabinKarp.class

public class RabinKarp { private int foundedAt = ; private String pattern ; private String text ; private long duration = ; public RabinKarpString pattern, String text { this . pattern = pattern; this . text = text; } Mengambil posisi index public int getFoundedAt { return foundedAt ; } Mengambil durasi pencarian public long getDuration { return duration ; } public boolean check { long startMillis = System.currentTimeMillis; long endMillis = ; boolean result = false ; double patternHash = getHash pattern ; int patternLength = pattern .length; int textLength = text .length; int startIndex = ; int endIndex = patternLength; while endIndex = textLength { String matchText = text .substringstartIndex, endIndex; double matchTextHash = getHashmatchText; A-5 Universitas Sumatera Utara apakah hash sesuai? if patternHash == matchTextHash { apakah seluruh karakter sesuai dengan pattern if pattern .equalsmatchText{ this . foundedAt = startIndex; hitung durasi endMillis = System.currentTimeMillis; this . duration = endMillis - startMillis; beritahu hasil pencarian ditemukan result = true ; break ; } } maju per satu index posisi karakter startIndex += 1 ; endIndex += 1 ; OR... startIndex += patternLength; endIndex += patternLength; } return result; } public double getHashString text { double hashValue = ; int base = 5 ; Log.iAndroidRuntime HASH, text; for long i = ; i text.length; i++ { char character = text.charAt int i; int ascii = int character; int exponent = int text.length- 1 -i; double pow = Math.powbase, exponent; double hash = ascii pow; Log.iAndroidRuntime RES, ascii + + base + + exponent + = + hash; hashValue += hash; A-6 Universitas Sumatera Utara } return hashValue; } }

5. MenuActivity.class