-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTemplate.java
More file actions
234 lines (208 loc) · 5.45 KB
/
StringTemplate.java
File metadata and controls
234 lines (208 loc) · 5.45 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.fqh;
/**
* @author ikun
* @version v1.0.0
* @since 2024/3/6 16:26
**/
import java.util.ArrayList;
import java.util.List;
/**
* 字符串算法相关模板
*/
public class StringTemplate {
public static void main(String[] args) {
String a = "ababab";
String b = "ab";
KMP kmp = new KMP();
int count = kmp.matchCount(a, b);
System.out.println(count);
int firstIndex = kmp.matchFirstIndex(a, b);
System.out.println(firstIndex);
List<Integer> matchAllIndex = kmp.matchAllIndex(a, b);
System.out.println(matchAllIndex);
}
}
/**
* 字符串哈希
*/
class StringHash {
static final int N = 100010;
static final int P = 131;
int n, m;
// p[i]=P^i, h[i]=s[1~i]的hash值
long[] p = new long[N];
long[] h = new long[N];
char[] s = new char[N];
// 预处理 hash函数的前缀和
void init() {
p[0] = 1;
h[0] = 0;
for (int i = 1; i <= n; i++) {
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + s[i];
}
}
// 计算s[l~r]的 hash值
long get(int l, int r) {
return h[r] - h[l - 1] * p[r - l + 1];
}
// 计算串的哈希值
long calc(char[] s, int n) {
h[0] = 0;
for (int i = 1; i <= n; i++) {
h[i] = h[i - 1] * P + s[i];
}
return h[n];
}
// 判断两子串是否相同
boolean substr(int l1, int r1, int l2, int r2) {
return get(l1, r1) == get(l2, r2);
}
public StringHash(int n, int m, String str) {
this.n = n;
this.m = m;
for (int i = 0; i < n; i++) {
s[i + 1] = str.charAt(i);
}
init();
}
}
/**
* KMP算法
*/
class KMP {
char[] s;
char[] p;
int n, m;
int[] next;
public KMP() {
}
/**
* 返回 b在a中匹配的第一次出现的位置
*/
int matchFirstIndex(String a, String b) {
this.n = a.length();
this.m = b.length();
a = " " + a;
b = " " + b;
this.s = a.toCharArray();
this.p = b.toCharArray();
this.next = new int[m + 1];
int[] next = new int[m + 1];
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && p[i] != p[j + 1]) j = next[j];
if (p[i] == p[j + 1]) j++;
next[i] = j;
}
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && s[i] != p[j + 1]) j = next[j];
if (s[i] == p[j + 1]) j++;
if (j == m) {
j = next[j];
return i - m;
}
}
return -1;
}
/**
* 返回 b在a中匹配的所有位置
*
* @return 如果这些下标会被用来查找,则返回TreeSet。
*/
List<Integer> matchAllIndex(String a, String b) {
this.n = a.length();
this.m = b.length();
a = " " + a;
b = " " + b;
this.s = a.toCharArray();
this.p = b.toCharArray();
this.next = new int[m + 1];
List<Integer> list = new ArrayList<>();
int[] next = new int[m + 1];
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && p[i] != p[j + 1]) j = next[j];
if (p[i] == p[j + 1]) j++;
next[i] = j;
}
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && s[i] != p[j + 1]) j = next[j];
if (s[i] == p[j + 1]) j++;
if (j == m) {
j = next[j];
list.add(i - m);
}
}
return list;
}
/**
* 返回b在a中匹配的次数
*/
int matchCount(String a, String b) {
this.n = a.length();
this.m = b.length();
a = " " + a;
b = " " + b;
int ans = 0;
this.s = a.toCharArray();
this.p = b.toCharArray();
this.next = new int[m + 1];
int[] next = new int[m + 1];
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && p[i] != p[j + 1]) j = next[j];
if (p[i] == p[j + 1]) j++;
next[i] = j;
}
for (int i = 1, j = 0; i <= n; i++) {
while (j > 0 && s[i] != p[j + 1]) j = next[j];
if (s[i] == p[j + 1]) j++;
if (j == m) {
j = next[j];
ans++;
}
}
return ans;
}
}
/**
* Z函数(扩展KMP)
*/
class ZFunc {
char[] s;
public ZFunc(String str) {
this.s = str.toCharArray();
}
public int[] getZ() {
int n = s.length;
int[] z = new int[n];
for (int i = 0, l = 0, r = 0; i < n; i++) {
if (i <= r && z[i - l] < r - i + 1) {
z[i] = z[i - 1];
} else {
z[i] = Math.max(0, r - i + 1);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
}
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
}
// 计算任意位置s[i:j]是否是一个回文字符串
class PalString2D {
public boolean[][] calc(String s) {
int n = s.length();
boolean[][] ok = new boolean[n][n];
for (int i = 0; i < 2 * n - 1; i++) {
int l = i / 2;
int r = i / 2 + i % 2;
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
ok[l][r] = true;
l--;
r++;
}
}
return ok;
}
}