結果
| 問題 |
No.263 Common Palindromes Extra
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-04-26 02:16:31 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,676 bytes |
| コンパイル時間 | 2,413 ms |
| コンパイル使用メモリ | 167,052 KB |
| 実行使用メモリ | 321,732 KB |
| 最終ジャッジ日時 | 2024-06-22 00:55:03 |
| 合計ジャッジ時間 | 5,272 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
long[2] cnt;
this(int id, int len, int fail) {
this.id = id;
this.len = len;
this.fail = fail;
}
}
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) {
nodes[i + 2] = nodes[nodes[u].next[a[i]]];
} else {
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]];
}
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;
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 += nodes[u].cnt[0] * nodes[u].cnt[1];
}
}
writeln(ans);
}
} catch (EOFException e) {
}
}