P4391 [BOI2009】無線伝送無線伝送

P4391 [BOI2009】無線送信無線伝送
少ないと考えることができる\(次\)魔法の右のアレイ...(ちょうど終了を繰り返す必要はない)最小サイクル部に被験者に回答であるという結論で開始。\(N - next_n \) なぜ?
に従って\(次\)アレイを画定するために、我々は知ることができる\(iは\) \(1〜next_i \)\(NEXT_ {I-next_i + 1}〜iは\)が同じであり、そしてもうある\(\次の)値この条件を満たす。
したがって、次の場合(I-next_i \)\割り切れる\(I \)場合、\を(I-next_i 1〜\)ので、最小の円形断面ではありません。被写体が完了したばかり、割り切れる判断するために必要とされる繰り返す必要はなく、直接出力することができます。
\(コード:\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

template < class T >
    inline void write (T x) {
       static T stk[100] , top = 0 ;
       if ( x == 0 ) { putchar ('0') ; return ; }
       if ( x < 0 ) { x = - x ; putchar ( '-' ) ; }
       while ( x ) { stk[++top] = x % 10 ; x /= 10 ; }
       while ( top ) { putchar ( stk[top--] + '0' ) ; }
       putchar ('\n') ;
    }

const int N = 1e6 + 100 ;

int n , nxt[N] ;
char s[N] ;

signed main (int argc , char * argv[]) {
    n = rint () ; scanf ("%s" , s + 1 ) ;
    int j = 0 ; rep ( i , 2 , n ) {
        while ( j && s[i] != s[j+1] ) j = nxt[j] ;
        if ( s[i] == s[j+1] ) ++ j ; nxt[i] = j ;
    }
    printf ("%lld\n" , n - nxt[n] ) ;
    system ("pause") ; return 0 ;
}

おすすめ

転載: www.cnblogs.com/Equinox-Flower/p/11480929.html