-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.java
157 lines (147 loc) · 3.2 KB
/
StringUtils.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.Arrays;
public class StringUtils {
public static String reverse(String s) {
int len = s.length();
char[] t = new char[len];
for (int i = 0; i < len; i++)
t[i] = s.charAt(len - 1 - i);
return new String(t);
}
public static boolean isPalindrome(String s) {
int len = s.length();
for (int i = 0; i < len / 2; i++)
if (s.charAt(i) != s.charAt(len - 1 - i))
return false;
return true;
}
public static int[] kmp(String s) {
int n = s.length();
int[] p = new int[n + 1];
p[0] = -1;
for (int i = 0; i < n; i++) {
int k = p[i];
char c = s.charAt(i);
while ((k >= 0) && (s.charAt(k) != c)) {
k = p[k];
}
p[i + 1] = k + 1;
}
return p;
}
static class Hashing {
final int X = 566239;
final int M = 1000000007;
final int[] h;
final int[] t;
public Hashing(String s) {
int n = s.length();
h = new int[n + 1];
t = new int[n + 1];
t[0] = 1;
for (int i = 0; i < n; i++) {
t[i + 1] = (int) ((t[i] * (long) X) % M);
h[i + 1] = (int) ((h[i] * (long) X + s.charAt(i)) % M);
}
}
public int hash(int from, int to) {
int res = (int) ((h[to] - h[from] * (long) t[to - from]) % M);
if (res < 0) {
res += M;
}
return res;
}
}
/**
* @author andrewzta and svembox
* @param s
* @return z-function of s
*/
public static int[] z(String s) {
int n = s.length();
int[] z = new int[n];
for (int i = 1, l = 0, r = 1; i < n; i++, r = Math.max(i, r)) {
for (z[i] = Math.min(r - i, z[i - l]); i + z[i] < n && s.charAt(i + z[i]) == s.charAt(z[i]); z[i]++, r = i + z[i], l = i) {
// enjoy
}
}
return z;
}
static class Ukkonen {
int[][] t;
int[] l;
int[] r;
int[] p;
int[] s;
Ukkonen(String a) {
int n = a.length();
t = new int[3 * n + 10][27];
l = new int[3 * n + 10];
r = new int[3 * n + 10];
p = new int[3 * n + 10];
s = new int[3 * n + 10];
for (int i = 0; i < t.length; i++) {
Arrays.fill(t[i], -1);
}
Arrays.fill(t[1], 0);
Arrays.fill(r, n - 1);
int ts = 2;
s[0] = 1;
l[0] = -1;
r[0] = -1;
l[1] = -1;
r[1] = -1;
int tv = 0;
int tp = 0;
for (int i = 0; i < n; i++) {
int c = a.charAt(i) - 'a';
for (;;) {
if (r[tv] < tp) {
if (t[tv][c] == -1) {
t[tv][c] = ts;
l[ts] = i;
p[ts++] = tv;
tv = s[tv];
tp = r[tv] + 1;
continue;
}
tv = t[tv][c];
tp = l[tv];
}
if (tp == -1 || c == a.charAt(tp) - 'a') {
tp++;
} else {
l[ts + 1] = i;
p[ts + 1] = ts;
l[ts] = l[tv];
r[ts] = tp - 1;
p[ts] = p[tv];
t[ts][c] = ts + 1;
t[ts][a.charAt(tp) - 'a'] = tv;
l[tv] = tp;
p[tv] = ts;
t[p[ts]][a.charAt(l[ts]) - 'a'] = ts;
ts += 2;
tv = s[p[ts - 2]];
tp = l[ts - 2];
while (tp <= r[ts - 2]) {
tv = t[tv][a.charAt(tp) - 'a'];
tp += r[tv] - l[tv] + 1;
}
if (tp == r[ts - 2] + 1) {
s[ts - 2] = tv;
} else {
s[ts - 2] = ts;
}
tp = r[tv] - tp + r[ts - 2] + 2;
continue;
}
break;
}
}
t = Arrays.copyOf(t, ts);
l = Arrays.copyOf(l, ts);
r = Arrays.copyOf(r, ts);
p = Arrays.copyOf(p, ts);
}
}
}