java - Knuth Morris Pratt application no error but not working -
here's application
"textform", take value searchbox.
"listkamus", take value array. "player name", change value string.
"kmp.knutmorris(textform, playername)", send textform, playername value knutmorris class
main class
public void ontextchanged(charsequence s, int arg1, int arg2, int arg3) { string textform = s.tostring(); searchresults.clear(); for(int i=0;i<listkamus.size();i++) { string playername=listkamus.get(i).tostring(); kmp.knutmorris(textform, playername); if(kmp.value==1){ searchresults.add(listkamus.get(i)); } } adapter.notifydatasetchanged(); }
kmp class
public class kmp { /** failure array **/ private int[] failure; public static int value; /** constructor **/ public kmp(string text, string pat) { /** pre construct failure array pattern **/ failure = new int[pat.length()]; fail(pat); /** find match **/ int pos = posmatch(text, pat); if (pos >= 0) { kmp.value = 1; } } /** failure function pattern **/ private void fail(string pat) { int n = pat.length(); failure[0] = -1; (int j = 1; j < n; j++) { int = failure[j - 1]; while ((pat.charat(j) != pat.charat(i + 1)) && >= 0) = failure[i]; if (pat.charat(j) == pat.charat(i + 1)) failure[j] = + 1; else failure[j] = -1; } } /** function find match pattern **/ private int posmatch(string text, string pat) { int = 0, j = 0; int lens = text.length(); int lenp = pat.length(); while (i < lens && j < lenp) { if (text.charat(i) == pat.charat(j)) { i++; j++; } else if (j == 0) i++; else j = failure[j - 1] + 1; } return ((j == lenp) ? (i - lenp) : -1); } /** main function **/ public static void knutmorris(string textform, string isidatabase) { string text = textform; string pattern = isidatabase; kmp kmp = new kmp(text, pattern); }
i want when people type on searchbox shows right list of array.
i think error 1 here
main class
if(kmp.value==1){ searchresults.add(listkamus.get(i)); }
or here
kmp class
if (pos >= 0) { kmp.value = 1; }
can tell me how fix ?
Comments
Post a Comment