Topic links: https://www.luogu.org/problem/P5410
Question is intended: to two strings a, b, b seeking a maximum prefix length for each of the suffixes a
Analysis: Extended KMP (also known as Z-algorithm algorithm) bare title
The blog to explain better: https://www.luogu.org/blog/lc-2018-Canton/solution-p5410
But there are several places he talked about a few problems, mainly in the case 2 inside
First, a start S [K + L] is definitely the back of p, the obvious
Case 2 red blue and green and then sequence the three lines is not the same, red and green is still the same, but the blue line will not wait, take a look at this myself
#include <bits / STDC ++ H.> the using namespace STD; typedef Long Long LL; const int MAXN = + 1E5 . 7 ; // interword add their own symbols, slightly enlarged const LL INF = 1e18; #define meminf (A) Memset (A, 0x3F, the sizeof (A)) #define MEM0 (A) Memset (A, 0, the sizeof (A)); char A [MAXN], B [MAXN]; int NXT [MAXN], Extend [MAXN]; // NXT [I] Representative b [i ... len] and b is the maximum prefix length, extend [i] representative of a [i ... len] and a maximum prefix length b void getnxt () { int len = strlen (B); NXT [ 0 ] = len; //[0] is the nxt original strings case, naturally, for the len int now = 0 ; the while (B [now] == B [now + . 1 ] && now + . 1 <len) now ++ ; nxt [ . 1 ] = now; int P0 = . 1 ; // P0 is satisfied p0 nxt [p0] -1 + maximum point, the beginning of initialization. 1 for ( int I = 2 ; I <len; I ++ ) { IF (I + NXT [P0-I] <+ P0 NXT [P0]) NXT [I] = NXT [I- P0]; // I corresponds to k + 1, nxt [i- p0] corresponds to L (nxt [k-p0 + 2) where the string is from 0 started, and the difference from a blog which is beginning a small increase 1 the else { int now NXT = P0 + [P0] - I; now=max(now,0);//防止i在p的后面 while(b[now]==b[i+now]&&i+now<len) now++; nxt[i]=now; p0=i;//更新p0 } } } void exkmp(){ getnxt(); int len=strlen(a); int now=0; while(a[now]==b[now]&&now<min(strlen(a),strlen(b))) now++; extend[0]=now; int p0=0; for(int i=1;i<len;i++){ if(i+nxt[i-p0]<extend[p0]+p0) extend[i]=nxt[i-p0]; else{ int now=p0+extend[p0]-i; now=max(now,0); while(b[now]==a[i+now]&&now<strlen(b)&&i+now<len) now++; extend[i]=now; p0=i; } } } int main(){ scanf("%s%s",a,b); exkmp(); for(int i=0;i<strlen(b);i++) printf("%d ",nxt[i]); putchar('\n'); for(int i=0;i<strlen(a);i++) printf("%d ",extend[i]); putchar('\n'); return 0; }