Luogu P5410 [template] extend the KMP

Meaning of the questions: seeking \ (T \) and \ (S \) longest common prefix for each suffix.

Shilling \ (S = S + T \) , \ (Z [I] \) represents \ (T \) and \ (suff_i \) longest common prefix.

(Note that subscript is from \ (0 \) begins)

\ (Z [I] = min (Z [IL], R & lt-I +. 1) \) , the front part of FIG:

After the part is to ensure that the right can not cross most borders, because further to the right part can not judge whether or not equal.

#include<iostream>
#include<cstdio>
#include<cstring>
#define R register int
using namespace std;
namespace Luitaryi {
const int N=100010;
int n,m,z[N<<1];
char s[N<<1],t[N];
inline void main() {
  scanf("%s",t);
  scanf("%s",s),n=strlen(s);
  s[n]='#',memcpy(s+n+1,t,strlen(t));
  for(R i=1,l=0,r;s[i];++i) {
    if(i<=r) z[i]=min(z[i-l],r-i+1);
    while(s[z[i]]==s[i+z[i]]) ++z[i];
    if(i+z[i]-1>r) r=i+z[i]-1,l=i;
  } printf("%d ",n);
  for(R i=1;i<n;++i) printf("%d ",z[i]);
  puts("");
  for(R i=n+1;s[i];++i) printf("%d ",z[i]);
}
} signed main() {Luitaryi::main(); return 0;}

2019.01.09

Guess you like

Origin www.cnblogs.com/Jackpei/p/12177373.html