結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-26 03:48:47
言語 D
(dmd 2.106.1)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,477 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 165,088 KB
実行使用メモリ 195,504 KB
最終ジャッジ日時 2024-06-22 00:55:51
合計ジャッジ時間 3,961 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
18,536 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 36 ms
43,444 KB
testcase_04 MLE -
testcase_05 MLE -
testcase_06 AC 17 ms
21,440 KB
testcase_07 AC 223 ms
191,844 KB
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(int E, char Offset) {
  string s;
  int n;
  int[] us, len, fail;
  int[] next;
  this(in string s) {
    this.s = s;
    n = cast(int)(s.length);
    us = new int[n + 3];
    len = new int[n + 3];
    fail = new int[n + 3];
    next = new int[(n + 3) << E];
    us[1] = 1; len[1] = -1; fail[1] = 1;
    us[2] = 2; len[2] =  0; fail[2] = 1;
    foreach (i; 0 .. n) {
      const c = s[i] - Offset;
      int u = us[i + 2];
      for (; !isSuffix(i, u); u = fail[u]) {}
      if (!next[u << E | c]) {
        next[u << E | 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 << E | c];
        }
      }
      us[i + 3] = next[u << E | c];
    }
  }
  bool isSuffix(int i, int u) const {
    const j = i - 1 - len[u];
    return (j >= 0 && s[j] == s[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 ~ ")")
                             : s[u - 2 - len[u] .. u - 2],
               u, fail[u]);
      const vs = next[u << E .. (u + 1) << E].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!(5, '?')(U);
      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