結果

問題 No.515 典型LCP
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-02 02:30:24
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 411 ms / 1,000 ms
コード長 3,602 bytes
コンパイル時間 769 ms
コンパイル使用メモリ 114,488 KB
実行使用メモリ 72,256 KB
最終ジャッジ日時 2023-09-04 01:30:53
合計ジャッジ時間 5,056 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 411 ms
68,568 KB
testcase_01 AC 315 ms
72,256 KB
testcase_02 AC 184 ms
51,828 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 158 ms
53,864 KB
testcase_06 AC 161 ms
52,100 KB
testcase_07 AC 162 ms
53,120 KB
testcase_08 AC 175 ms
54,312 KB
testcase_09 AC 164 ms
52,856 KB
testcase_10 AC 167 ms
51,596 KB
testcase_11 AC 165 ms
53,196 KB
testcase_12 AC 165 ms
52,812 KB
testcase_13 AC 166 ms
53,788 KB
testcase_14 AC 14 ms
5,408 KB
testcase_15 AC 158 ms
53,416 KB
testcase_16 AC 162 ms
51,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum LIM_LOGN = 17;

int N;
string[] S;
int M;
long X, D;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      S = new string[N];
      foreach (i; 0 .. N) {
        S[i] = readToken();
      }
      M = readInt();
      X = readLong();
      D = readLong();
      
      /*
      for k in 1 .. M
        i[k] = (x / (n - 1)) + 1
        j[k] = (x % (n - 1)) + 1
        if (i[k] > j[k])
          swap(i[k], j[k])
        else
          j[k] = j[k] + 1
        end
        x = (x + d) % (n * (n - 1))
      end
      */
      auto I = new long[M + 1];
      auto J = new long[M + 1];
      long x = X, d = D;
      foreach (k; 1 .. M + 1) {
        I[k] = (x / (N - 1)) + 1;
        J[k] = (x % (N - 1)) + 1;
        if (I[k] > J[k]) {
          swap(I[k], J[k]);
        } else {
          J[k] = J[k] + 1;
        }
        x = (x + d) % (1L * N * (N - 1));
      }
      debug {
        writeln("I = ", I);
        writeln("J = ", J);
      }
      
      auto L = new int[N];
      foreach (i; 0 .. N) {
        L[i] = cast(int)(S[i].length);
      }
      
      auto ord = iota(N).array.sort!((i, j) => (S[i] < S[j]));
      auto dro = new int[N];
      foreach (k; 0 .. N) {
        dro[ord[k]] = k;
      }
      auto lcp = new int[N - 1];
      foreach (k; 0 .. N - 1) {
        const i = ord[k];
        const j = ord[k + 1];
        int l;
        for (l = 0; l < L[i] && l < L[j] && S[i][l] == S[j][l]; ++l) {}
        lcp[k] = l;
      }
      debug {
        writeln(ord.map!(i => S[i]));
        writeln("lcp = ", lcp);
      }
      
      auto mn = new int[][](LIM_LOGN, N - 1);
      mn[0][] = lcp[];
      foreach (e; 1 .. LIM_LOGN) {
        mn[e][] = mn[e - 1][];
        foreach (k; 0 .. N - 1 - (1 << (e - 1))) {
          chmin(mn[e][k], mn[e - 1][k + (1 << (e - 1))]);
        }
      }
      debug {
        writeln("mn = ", mn);
      }
      
      long ansSum;
      foreach (m; 1 .. M + 1) {
        int i = cast(int)(I[m] - 1);
        int j = cast(int)(J[m] - 1);
        if (dro[i] > dro[j]) {
          swap(i, j);
        }
        int ans;
        if (i == j) {
          ans = L[i];
        } else {
          const e = bsr(dro[j] - dro[i]);
          ans = min(mn[e][dro[i]], mn[e][dro[j] - (1 << e)]);
          debug {
            writeln(i, " ", j, ": ", dro[i], " ", dro[j], ": ", ans);
          }
        }
        ansSum += ans;
      }
      writeln(ansSum);
    }
  } catch (EOFException e) {
  }
}
0