結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-26 02:29:57
言語 D
(dmd 2.106.1)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,990 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 164,988 KB
実行使用メモリ 196,120 KB
最終ジャッジ日時 2023-09-04 00:47:35
合計ジャッジ時間 3,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 10 ms
5,408 KB
testcase_04 AC 43 ms
16,252 KB
testcase_05 AC 37 ms
15,020 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 113 ms
61,272 KB
testcase_08 AC 107 ms
61,264 KB
testcase_09 AC 194 ms
105,908 KB
testcase_10 MLE -
testcase_11 AC 33 ms
14,764 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)); }

template Eertree(T) {
  class Node {
    int id, len, fail;
    // int[T] next;
    int[28] next;
    int[2] cnt;
    this(int id, int len, int fail) {
      this.id = id;
      this.len = len;
      this.fail = fail;
      this.next[] = -1;
    }
  }
  Node[] build(in T[] a) {
    const n = cast(int)(a.length);
    auto nodes = new Node[n + 2];
    nodes[0] = new Node(0, -1, 0);
    nodes[1] = new Node(1, 0, 0);
    bool isSuffix(int i, int u) {
      const j = i - 1 - nodes[u].len;
      return (j >= 0 && a[j] == a[i]);
    }
    foreach (i; 0 .. n) {
      int u = i + 1;
      for (; !isSuffix(i, u); u = nodes[u].fail) {}
      // if (a[i] in nodes[u].next) {
      if (nodes[u].next[a[i] - '?'] != -1) {
        // nodes[i + 2] = nodes[nodes[u].next[a[i]]];
        nodes[i + 2] = nodes[nodes[u].next[a[i] - '?']];
      } else {
        // nodes[u].next[a[i]] = i + 2;
        nodes[u].next[a[i] - '?'] = i + 2;
        int f;
        if (u == 0) {
          f = 1;
        } else {
          int v = nodes[u].fail;
          for (; !isSuffix(i, v); v = nodes[v].fail) {}
          // f = nodes[v].next[a[i]];
          f = nodes[v].next[a[i] - '?'];
        }
        nodes[i + 2] = new Node(i + 2, nodes[u].len + 2, f);
      }
    }
    return nodes;
  }
  void print(in Node[] nodes, in T[] a) {
    void dfs(int u, string prefix, int type) {
      writefln("%s%s%s %s %s %s",
               prefix, ["", "|-- ", "`-- "][type],
               (nodes[u].len <= 0) ? ("(" ~ nodes[u].len.to!string ~ ")") :
               a[nodes[u].id - 1 - nodes[u].len .. nodes[u].id - 1].to!string,
               nodes[u].id, nodes[u].fail,
               nodes[u].cnt);
      // const vs = nodes[u].next.values;
      const vs = nodes[u].next[].filter!(v => (v != -1)).array;
      foreach (v; vs) {
        dfs(v, prefix ~ ["", "|   ", "    "][type], (v == vs[$ - 1]) ? 2 : 1);
      }
    }
    dfs(0, "", 0);
    dfs(1, "  ", 0);
  }
}

string S, T;

void main() {
  try {
    for (; ; ) {
      S = readToken();
      T = readToken();
      
      const U = S ~ "?@" ~ T;
      auto nodes = Eertree!(char).build(U);
      const n = cast(int)(U.length);
      const m = cast(int)(S.length);
      foreach (i; 0 .. m) {
        ++nodes[i + 2].cnt[0];
      }
      foreach (i; m + 2 .. n) {
        ++nodes[i + 2].cnt[1];
      }
      foreach_reverse (u; 0 .. n + 2) {
        if (nodes[u].id == u) {
          const f = nodes[u].fail;
          foreach (j; 0 .. 2) {
            nodes[f].cnt[j] += nodes[u].cnt[j];
          }
        }
      }
      debug {
        writeln(U);
        Eertree!(char).print(nodes, U);
      }
      long ans;
      foreach (u; 2 .. n + 2) {
        if (nodes[u].id == u) {
          ans += 1L * nodes[u].cnt[0] * nodes[u].cnt[1];
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0