結果

問題 No.515 典型LCP
ユーザー 0w1
提出日時 2017-05-12 18:31:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 605 ms / 1,000 ms
コード長 1,539 bytes
コンパイル時間 1,826 ms
コンパイル使用メモリ 171,984 KB
実行使用メモリ 16,640 KB
最終ジャッジ日時 2024-09-15 10:44:07
合計ジャッジ時間 9,009 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAXN = int( 1e5 );
const int MAXLEN = int( 8e5 );

const int BASE = 311;
const int MOD = int( 1e9 ) + 7;

int N;
string S[ MAXN ];
int pbase[ MAXLEN + 1 ];
vector< int > phash[ MAXN ];

long long M, X, D;

pair< int, int > get_nxt() {
  int i = ( X / ( N - 1 ) ) + 1;
  int j = ( X % ( N - 1 ) ) + 1;
  if( i > j ) {
    swap( i, j );
  } else {
    j = j + 1;
  }
  X = ( X + D ) % ( 1LL * N * ( N - 1 ) );
  return make_pair( i - 1, j - 1 );
}

int gh( int x, int lb, int rb ) { // [ lb, rb )
  int res = ( phash[ x ][ rb ] - 1LL * phash[ x ][ lb ] * pbase[ rb - lb ] % MOD ) % MOD;
  if( res < 0 ) res += MOD;
  return res;
}

int get_lcp( int a, int b ) {
  int lb = 0, ub = min( S[ a ].size(), S[ b ].size() ) + 1;
  while( lb + 1 != ub ) {
    int mid = lb + ub >> 1;
    int ha = gh( a, 0, mid );
    int hb = gh( b, 0, mid );
    ( ha == hb ? lb : ub ) = mid;
  }
  return lb;
}

signed main() {
  for( int i = pbase[ 0 ] = 1; i <= MAXLEN; ++i ) {
    pbase[ i ] = 1LL * pbase[ i - 1 ] * BASE % MOD;
  }
  ios::sync_with_stdio( 0 );
  cin >> N;
  for( int i = 0; i < N; ++i ) {
    cin >> S[ i ];
    phash[ i ].assign( S[ i ].size() + 1, 0 );
    for( int j = 0; j < S[ i ].size(); ++j ) {
      phash[ i ][ j + 1 ] = ( 1LL * phash[ i ][ j ] * BASE + S[ i ][ j ] ) % MOD;
    }
  }
  cin >> M >> X >> D;
  long long ans = 0;
  for( int qi = 0; qi < M; ++qi ) {
    int a, b;
    tie( a, b ) = get_nxt();
    ans += get_lcp( a, b );
  }
  cout << ans << endl;
  return 0;
}
0