結果

問題 No.515 典型LCP
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-02 02:26:06
言語 D
(dmd 2.107.1)
結果
WA  
実行時間 -
コード長 3,606 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 114,324 KB
実行使用メモリ 70,160 KB
最終ジャッジ日時 2023-09-04 01:30:41
合計ジャッジ時間 5,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 184 ms
51,048 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 156 ms
51,596 KB
testcase_06 AC 159 ms
52,832 KB
testcase_07 AC 159 ms
52,780 KB
testcase_08 AC 177 ms
51,836 KB
testcase_09 AC 163 ms
51,260 KB
testcase_10 AC 163 ms
52,788 KB
testcase_11 AC 162 ms
51,012 KB
testcase_12 AC 163 ms
52,804 KB
testcase_13 AC 165 ms
51,900 KB
testcase_14 AC 16 ms
4,376 KB
testcase_15 AC 157 ms
54,300 KB
testcase_16 AC 157 ms
52,836 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) % (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) {
        const i = cast(int)(I[m] - 1);
        const j = cast(int)(J[m] - 1);
        int kI = dro[i];
        int kJ = dro[j];
        if (kI > kJ) {
          swap(kI, kJ);
        }
        int ans;
        if (kI == kJ) {
          ans = L[i];
        } else {
          const e = bsr(kJ - kI);
          ans = min(mn[e][kI], mn[e][kJ - (1 << e)]);
          debug {
            writeln(kI, " ", kJ, ": ", ans);
          }
        }
        ansSum += ans;
      }
      writeln(ansSum);
    }
  } catch (EOFException e) {
  }
}
0