package Algorithm_Class;
import java.util.Scanner;
public class StringSearch {
public static boolean RabinKarp(String txt,String pat) {
int txthash = 0;
int pathash = 0;
int txtlength = txt.length();
int patlength = pat.length();
for(int i = patlength;i > 0;i--) {
pathash += ((int)pat.charAt(patlength - i)) * Math.pow(2,i - 1);
}
System.out.println("pathash : " + pathash);
for(int i = 0;i <= txtlength - patlength;i++) {
if(i == 0) {
for(int j = patlength; j > 0; j--) {
txthash += (int)Math.pow(2,j - 1) * (int)txt.charAt(patlength - j);
}
System.out.println("i == 0 : " + txthash);
} else {
txthash = 2 * (txthash - (int)txt.charAt(i - 1) * (int)Math.pow(2,patlength - 1)) + (int)txt.charAt(i + patlength -1);
}
if(txthash == pathash)
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(RabinKarp("ABACDSDFSDGSDGDFJGFDKVNCXMVXCWIERWETUFGDKJGHKSDFALDJVCKLNX","XMVXCWIE"));
}
}
ㅁㅌㅊ?
댓글 0