BF算法
BF算法,即暴力(Brute Force)算法,是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果。BF算法是一种蛮力算法
代码如下:
/***
* 暴力算法
* @param pat
* @param org
* @return
*/
public static int bruteForce(String pat,String org,int position) {
byte[] patter = pat.getBytes();
byte[] text = org.getBytes();
int pl = patter.length;
int tl = text.length;
int result = -1;
int max = tl - pl;
if (position < 0 || pl > tl || max - position < 0) {
return -1;
}
for (int i = position; i <= max; i++) {
int count = 0;
for (int j = 0; j < pl; j++) {
if (patter[j] == text[i + j]) {
++count;
}
}
if (count == pl) {
result = i;
break;
}
}
return result;
}
RK算法
代码如下:
/***
* hashcode算法
* @param patter
* @param text
* @return
*/
public static int hashCode(String patter,String text,int position) {
int result = -1;
int length = patter.length();
int max = text.length() - length;
if (position < 0 || max < 0||max-position<0) {
return result;
}
int patterHash = patter.hashCode();
byte[] pat = patter.getBytes(StandardCharsets.UTF_8);
for (int i = position; i <= max; i++) {
String sub = text.substring(i, length + i);
int subHash = sub.hashCode();
int count = 0;
if (patterHash == subHash) {
byte[] org = sub.getBytes(StandardCharsets.UTF_8);
for (int j = 0; j < pat.length; j++) {
if (pat[j] == org[j]) {
++count;
}
}
}
if (count == length) {
result = i;
break;
}
}
return result;
}
如有不对之处请多多指正!谢谢!