結果

問題 No.430 文字列検索
ユーザー te-shte-sh
提出日時 2017-10-27 17:32:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 91,008 KB
実行使用メモリ 6,976 KB
最終ジャッジ日時 2023-09-03 16:31:37
合計ジャッジ時間 2,509 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 37 ms
5,872 KB
testcase_02 AC 83 ms
5,020 KB
testcase_03 AC 85 ms
4,972 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 33 ms
4,932 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 46 ms
5,840 KB
testcase_12 AC 47 ms
4,876 KB
testcase_13 AC 47 ms
6,976 KB
testcase_14 AC 46 ms
4,972 KB
testcase_15 AC 50 ms
5,056 KB
testcase_16 AC 56 ms
4,972 KB
testcase_17 AC 68 ms
4,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto s = readln.chomp, n = s.length;
  auto sa = SuffixArray(s);
  auto saa = new string[](n);
  foreach (i; 0..n) saa[i] = sa[i];
  auto sas = saa.assumeSorted;

  auto m = readln.chomp.to!size_t, ans = size_t(0);
  foreach (_; 0..m) {
    auto c = readln.chomp;
    auto r1 = sas.lowerBound(c);
    auto r2 = sas.upperBound(c ~ "|");
    ans += n - r1.length - r2.length;
  }

  writeln(ans);
}

struct SuffixArray
{
  import std.algorithm;

  string s;
  size_t n;
  size_t[] x;

  this(string s)
  {
    this.s = s;
    n = s.length;
    x = new size_t[](n);
    auto r = new size_t[](n), t = new size_t[](n);

    foreach (i; 0..n) r[x[i] = i] = s[i];
    for (size_t h = 1; t[n-1] != n-1; h <<= 1) {
      auto cmp(size_t i, size_t j)
      {
        if (r[i] != r[j]) return r[i] < r[j];
        return i+h < n && j+h < n ? r[i+h] < r[j+h] : i > j;
      }
      x.sort!((a, b) => cmp(a, b));
      foreach (i; 0..n-1) t[i+1] = t[i] + cmp(x[i], x[i+1]);
      foreach (i; 0..n) r[x[i]] = t[i];
    }
  }

  auto opIndex(size_t i) { return s[x[i]..$]; }
}
0