Brief introduction
z [i]: for i and the position of the original prefix string of the same length is the number
p [] + "#" + s [] - matching strings found for each text string of the same position and its prefix
Processing array z
Z [0] = 0 is not valid, with z [k] (k <i) to Release z [i]
Z look quite good before there can cover the current i, and the right boundary of the right cover better l, r is the boundary box
if (no previous zbox to cover) the enumeration process had to answer this one from i violence
else if (has) s [i] can be overwritten so s [i] is inevitable in the s [0 ~ z [j] - 1] j at the beginning of this section intended to cover the maximum current is maintained to the right boundary i
So we will now go to find the position s [i] is mapped in the original string, i.e., i - l
At this time Obviously, it s in the box range [i ~ r] and the mapping s [il ~ rl] is same length of this section is L = r - i + 1
It is easy to see two types of situation:
1.z [i - l] <L can be derived directly z [i] = z [i - l]
2. The excess part of the border had to find an enumeration of violence
template
// LEN-- text strings match string len--
void getz(){
int l = 0,r = 0;
z[0] = 0;
for(int i = 1;i <= LEN + len; i++){
if(i > r){
int n = 0;
while(p[n] == p[i + n]) n++;
if(n){
l = i;
r = i + n - 1;
}
z[i] = n;
}
else{
if(z[i - l] < r - i + 1) z[i] = z[i - l];
else{
int n = 1;
while(p[r - i + n] == p[r + n]) n++;
r = r + n - 1;
l = i;
z[i] = r - l + 1;
}
}
}
}