多くの等しい部分文字列CF-1029A(KMP)

 

 トピック:小文字のSのn文字列を与え、k Sを見つけることができる新しい文字列を見つけ、それをできるだけ短くします

nxt配列の意味を調査する

#include <iostream> 
#include <cstring> 
#include <cstdio> 
#include <algorithm>
 using  namespace std;
const  int maxn = 4e5 + 10 ;
int n、k;
char s [maxn];
int l;
int nxt [maxn];
int ans [maxn];
void work(){
     forint i = 2、j = 0 ; i <= l; i ++ ){
         while(j> 0 && s [i]!= s [j + 1 ])
            j= nxt [j];
        if(s [i] == s [j + 1 ])j ++ ; 
        nxt [i] = j; 
    } 
} 



int main(){
     // freopen( "in"、 "r"、stdin); 
    scanf(" %d%d "、&​​n、&k); 
   scanf(" %s "、s + 1 ); 
   l = strlen(s + 1 ); 
   作業(); 
   printf(" %s "、s + 1 ); 
   K - ;
   forint i = nxt [l] + 1 ; i <= l; i ++ 
           printf(" %c " 、s [i]); 
    } 
    printf(" \ n " );
    0を返し ます
}
コードを表示

 

おすすめ

転載: www.cnblogs.com/xcfxcf/p/12742145.html