-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZfunction.java
More file actions
39 lines (34 loc) · 925 Bytes
/
Copy pathZfunction.java
File metadata and controls
39 lines (34 loc) · 925 Bytes
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
package templates;
public class Zfunction {
int[] zvalues(StringBuilder s){
int n=s.length();
int[] zvals=new int[n];
int l=0,r=0;
for(int i=1;i<n;i++){
if(i<r){
zvals[i]=Math.min(zvals[i-l],r-i);
}
while(i+zvals[i]<n && s.charAt(i+zvals[i])==s.charAt(zvals[i]))zvals[i]++;
if(i+zvals[i]>r){
l=i;
r=i+zvals[i];
}
}
return zvals;
}
public int match(StringBuilder pattern,StringBuilder text){
int ans=-1;
StringBuilder tot=new StringBuilder(pattern);
tot.append('#');
tot.append(text);
int n=pattern.length();
int[] z=zvalues(tot);
for(int i=0;i<tot.length();i++){
if(z[i]==n){
ans=i-(n+1);
break;
}
}
return ans;
}
}