kmp Luo Gu P3375

Topic links: https://www.luogu.org/problem/P3375

Is a bare kmp, remember that before long, long time, first look kmp, looking for a day did not understand, or impetuous increase efficiency is too low, kmp a good look in fact it is not difficult at all

Template code live it.

#include <bits / STDC ++ H.>
 the using  namespace STD;
 const  int MAXN = 1e6 + 10 ;
 const  int INF = 0x3f3f3f3f ; 
typedef Long  Long LL;
 #define meminf (A) Memset (A, 0x3F, the sizeof (A))
 #define MEM0 (a) Memset (a, 0, the sizeof (a));
 char S1 [MAXN], S2 [MAXN];
 int ; NXT [MAXN] // mismatch when used after recording mode is matched to the i-th bit string , a position to which the jump mode string 
int main () { 
    Scanf ( " % S% S " , S1 + . 1 , S2 + . 1 );
     //Scanf ( "% S", S2 +. 1); 
    int LEN1 = strlen (S1 + . 1 ), LEN2 = strlen (S2 + . 1 );
     // Next seeking nxt array, nxt array determined only by the pattern string itself 
    nxt [ 0 ] = 0 , NXT [ . 1 ] = 0 ; // first, a second mismatch, only the first one as possible to the beginning of a new 
    int K = 0 ; // K points to the next movement of the pointer j position 
     // COUT 233 << << endl; 
    for ( int I = 2 ; I <= LEN2; I ++ ) {
         // this is the second bit from the pattern string matching the start of 
        the while (S2 [I] = S2 [! + K . 1 ] && K! = 0 ) = KNXT [K]; 
         IF (S2 [I] == S2 [K + . 1 ]) NXT [I] = ++ K; 
    } 
    
    K = 0 ; 
     for ( int I = . 1 ; I <= LEN1; I ++ ) {
         the while ( ! K && S1 [I] = S2 [K + . 1 ]) K = nxt [K]; // If not, then the use of nxt array jumped back 
        IF (S1 [I] == S2 [K + . 1 ]) K ++; // If they are equal, the next one is matched 
        IF (K == LEN2) the printf ( " % D \ n- " , I-LEN2 + . 1 );
         // If they are equal, we start position outputs 
    }
     for ( int I =1;i<len2;i++)printf("%d ",nxt[i]);
    printf("%d\n",nxt[len2]);
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11372898.html