結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-20 16:48:48
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 3,496 bytes
コンパイル時間 2,924 ms
コンパイル使用メモリ 163,176 KB
実行使用メモリ 177,432 KB
最終ジャッジ日時 2023-09-04 01:07:27
合計ジャッジ時間 4,601 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
19,840 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 41 ms
41,640 KB
testcase_04 AC 240 ms
177,056 KB
testcase_05 AC 242 ms
177,432 KB
testcase_06 AC 20 ms
20,292 KB
testcase_07 AC 226 ms
176,172 KB
testcase_08 AC 215 ms
175,596 KB
testcase_09 AC 224 ms
175,672 KB
testcase_10 AC 231 ms
175,596 KB
testcase_11 AC 206 ms
176,132 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