結果
| 問題 |
No.263 Common Palindromes Extra
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-26 02:29:57 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
MLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,990 bytes |
| コンパイル時間 | 2,035 ms |
| コンパイル使用メモリ | 166,928 KB |
| 実行使用メモリ | 195,572 KB |
| 最終ジャッジ日時 | 2024-06-22 00:55:25 |
| 合計ジャッジ時間 | 3,531 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 MLE * 1 |
ソースコード
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) {
}
}