-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_strStr.java
140 lines (123 loc) · 2.79 KB
/
Solution_strStr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.bupt.kcrosswind.Leetcode;
public class Solution_strStr {
// 暴力法
public static String strStr(String haystack, String needle) {
int i = 0;
int hlength = haystack.length();
int nlength = needle.length();
if (nlength == 0) {
return haystack;
} else {
while (i <= hlength - nlength) {
int j = 0;
while (j < nlength) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
break;
} else if (j == nlength - 1) {
return haystack.substring(i);
}
j++;
}
i++;
}
return null;
}
}
// kmp算法 ,效率极低
public static String strStrKMP(String haystack, String needle) {
int nlength = needle.length();
if (nlength == 0) {
return haystack;
}
// int[] form = KMPform(needle);
int[] form = KMPformjuly(needle);
int hlength = haystack.length();
int i = 0;//需要跳转i,所以用while
while (i <= hlength - nlength) {
int j = 0;
while (j < nlength) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
if (j == 0) {
i++;
} else {
i = i + j - form[j - 1];
}
break;
} else if (j == nlength - 1) {
return haystack.substring(i);// 左闭右开
}
j++;
}
}
return null;
}
public static int[] KMPform(String str) {
int[] form = new int[str.length()];
for (int i = 0; i < form.length; i++) {
int j = i;
while (j > 0) {
if (str.substring(0, j).equals(str.substring(i - j + 1, i + 1))) {
form[i] = j;
break;
}
j--;
}
if (j == 0) {
form[i] = 0;
}
}
return form;
}
public static int[] KMPformjuly(String str) {
//
// int[] form = new int[str.length()];
// form[0] = -1;
// for (int i = 1; i < form.length; i++) {
//
// if (str.charAt(form[i - 1] + 1) == str.charAt(i)) {
// form[i] = form[i - 1] + 1;
// } else {
// int j = form[i - 1];
// while (j > 0) {
// if (str.substring(0, j).equals(str.substring(i - j + 1, i + 1))) {
// form[i] = j;
// break;
// }
//
// j--;
// }
// if (j == 0) {
// form[i] = -1;
// }
// }
// }
// return form;
int[] form = new int[str.length()];
form[0] = -1;
for (int i = 1; i < form.length; i++) {
int index = form[i - 1];
while (index >= 0 && str.charAt(i) != str.charAt(index + 1)) {
index = form[index];// 完全不懂原理是不是以最后两位为基准一直往上推???
}
if (str.charAt(i) == str.charAt(index + 1)) {
form[i] = index + 1;// =form[i-1]+1//实际上是不对的 还是要用index+1
}
else {
form[i] = -1;
}
}
return form;
}
public static void main(String[] args) {
int[] A = { 1, 1 };
String s = "mississippi";
String ss = "issip";
String sss = "ABCDABD";
int[] f = KMPformjuly(sss);
System.out.println(f.length);
for (int i = 0; i < f.length; i++) {
System.out.println(f[i]);
}
System.out.println(strStrKMP(s, ss));
}
}