結果

問題 No.515 典型LCP
ユーザー 0w10w1
提出日時 2017-05-12 18:31:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 651 ms / 1,000 ms
コード長 1,539 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 169,540 KB
実行使用メモリ 16,700 KB
最終ジャッジ日時 2023-10-13 13:57:02
合計ジャッジ時間 9,429 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 651 ms
16,636 KB
testcase_01 AC 609 ms
16,700 KB
testcase_02 AC 374 ms
16,096 KB
testcase_03 AC 10 ms
11,992 KB
testcase_04 AC 10 ms
12,184 KB
testcase_05 AC 403 ms
16,048 KB
testcase_06 AC 412 ms
15,940 KB
testcase_07 AC 403 ms
16,032 KB
testcase_08 AC 412 ms
16,164 KB
testcase_09 AC 364 ms
15,924 KB
testcase_10 AC 155 ms
15,992 KB
testcase_11 AC 154 ms
15,960 KB
testcase_12 AC 155 ms
16,036 KB
testcase_13 AC 447 ms
15,920 KB
testcase_14 AC 21 ms
15,904 KB
testcase_15 AC 494 ms
15,956 KB
testcase_16 AC 499 ms
15,948 KB
権限があれば一括ダウンロードができます

ソースコード

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