結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-20 16:48:48
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 3,496 bytes
コンパイル時間 2,526 ms
コンパイル使用メモリ 169,056 KB
実行使用メモリ 179,132 KB
最終ジャッジ日時 2024-06-22 01:14:11
合計ジャッジ時間 4,775 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
16,740 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 42 ms
40,068 KB
testcase_04 AC 217 ms
177,108 KB
testcase_05 AC 206 ms
176,184 KB
testcase_06 AC 20 ms
19,956 KB
testcase_07 AC 220 ms
176,744 KB
testcase_08 AC 213 ms
179,132 KB
testcase_09 AC 220 ms
177,224 KB
testcase_10 AC 225 ms
178,184 KB
testcase_11 AC 196 ms
177,084 KB
権限があれば一括ダウンロードができます

ソースコード

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 size, 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) * size];
    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 * size + c]) {
        next[u * size + 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 * size + c];
        }
      }
      us[i + 3] = next[u * size + 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 * size .. (u + 1) * size].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!(28, '?')(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