結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-26 03:41:14
言語 D
(dmd 2.106.1)
結果
MLE  
実行時間 -
コード長 3,455 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 165,548 KB
実行使用メモリ 215,464 KB
最終ジャッジ日時 2024-06-22 00:55:35
合計ジャッジ時間 4,867 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
19,976 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 60 ms
47,204 KB
testcase_04 MLE -
testcase_05 MLE -
testcase_06 AC 29 ms
22,292 KB
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, 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)); }

class Eertree {
  string a;
  int n;
  int[] us, len, fail;
  int[][] next;
  this(in string a, int alphabetSize, char offset) {
    this.a = a;
    n = cast(int)(a.length);
    us = new int[n + 3];
    len = new int[n + 3];
    fail = new int[n + 3];
    next = new int[][](n + 3, alphabetSize);
    us[1] = 1; len[1] = -1; fail[1] = 1;
    us[2] = 2; len[2] = 0; fail[2] = 1;
    foreach (i; 0 .. n) {
      const c = a[i] - offset;
      int u = us[i + 2];
      for (; !isSuffix(i, u); u = fail[u]) {}
      if (!next[u][c]) {
        next[u][c] = i + 3;
        len[i + 3] = len[u] + 2;
        if (u == 1) {
          fail[i + 3] = 2;
        } else {
          int v = fail[u];
          for (; !isSuffix(i, v); v = fail[v]) {}
          fail[i + 3] = next[v][c];
        }
      }
      us[i + 3] = next[u][c];
    }
  }
  bool isSuffix(int i, int u) const {
    const j = i - 1 - len[u];
    return (j >= 0 && a[j] == a[i]);
  }
  void print() const {
    void dfs(int u, string prefix, int type) {
      writefln("%s%s%s %s %s", prefix, ["", "|-- ", "`-- "][type],
               (len[u] <= 0) ? ("(" ~ len[u].to!string ~ ")")
                             : a[u - 2 - len[u] .. u - 2],
               u, fail[u]);
      const vs = next[u].filter!(v => v).array;
      foreach (v; vs) {
        dfs(v, prefix ~ ["", "|   ", "    "][type], (v == vs[$ - 1]) ? 2 : 1);
      }
    }
    dfs(1, "", 0);
    dfs(2, "  ", 0);
  }
}

string S, T;

void main() {
  try {
    for (; ; ) {
      S = readToken();
      T = readToken();
      
      const U = S ~ "?@" ~ T;
      auto eertree = new Eertree(U, 28, '?');
      debug {
        eertree.print;
      }
      const n = cast(int)(U.length);
      const m = cast(int)(S.length);
      auto cnt = new long[][](n + 3, 2);
      foreach (i; 0 .. m) {
        ++cnt[eertree.us[i + 3]][0];
      }
      foreach (i; m + 2 .. n) {
        ++cnt[eertree.us[i + 3]][1];
      }
      foreach_reverse (u; 1 .. n + 3) {
        if (eertree.us[u] == u) {
          foreach (j; 0 .. 2) {
            cnt[eertree.fail[u]][j] += cnt[u][j];
          }
        }
      }
      long ans;
      foreach (u; 3 .. n + 3) {
        if (eertree.us[u] == u) {
          ans += cnt[u][0] * cnt[u][1];
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0