public class Test {
//朴素模式匹配算法
public int Index(String s,String t,int pos){
int i = pos;//主串中第几个位置开始比较
int j = 0;//模式串中的第一个位置
while(i<>=t.length()) {
return i-t.length();
}else {
return -1;
}
}
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.Index(goodgoogle, google, 0));
System.out.println(test.Index(goodgoogle, google, 2));
System.out.println(test.Index(googlegoogle, google, 0));
System.out.println(test.Index(googlegoogle, google, 2));
}
}
运行结果:
4
4
0
6
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.getNext(abcabx);
test.getNext(abcdex);
System.out.println(test.indexKMP(goodgoogle, google, 0));
}
//////////KMP模式匹配算法////////////////
public int[] getNext(String T){
int[] next = new int[T.length()];
int i = 0;
int j = -1;
next[i] = j;
while(i=T.length()) {
return i-T.length();
}else {
return 0;
}
}
}
运行结果:
-100012
-100000
-100010
4