結果
| 問題 | No.263 Common Palindromes Extra |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-25 19:54:26 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 15 ms / 2,000 ms |
| + 720µs | |
| コード長 | 4,237 bytes |
| 記録 | |
| コンパイル時間 | 3,395 ms |
| コンパイル使用メモリ | 361,780 KB |
| 実行使用メモリ | 16,144 KB |
| 最終ジャッジ日時 | 2026-11-25 19:54:33 |
| 合計ジャッジ時間 | 5,541 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
class PalindromicTree {
struct Node {
int length, link = 0, transition = 0;
uint32_t countS = 0, countT = 0;
unsigned char outerLetter;
Node(int length, int letter)
: length(length), outerLetter(static_cast<unsigned char>(letter)) {}
};
vector<Node> nodes;
vector<array<int, 26>> branching;
// Zero: no edges. Positive: one child. Negative: a 26-entry table.
int next(int vertex, int letter) const {
int descriptor = nodes[vertex].transition;
if (descriptor == 0) return 0;
if (descriptor > 0)
return nodes[descriptor].outerLetter == letter ? descriptor : 0;
return branching[-descriptor - 1][letter];
}
void addEdge(int vertex, int letter, int child) {
int descriptor = nodes[vertex].transition;
if (descriptor == 0) {
nodes[vertex].transition = child;
} else if (descriptor > 0) {
int table = static_cast<int>(branching.size());
branching.push_back({});
branching.back()[nodes[descriptor].outerLetter] = descriptor;
branching.back()[letter] = child;
nodes[vertex].transition = -table - 1;
} else {
branching[-descriptor - 1][letter] = child;
}
}
int extendableSuffix(int vertex, int position, const string& text) const {
while (true) {
int left = position - nodes[vertex].length - 1;
if (left >= 0 && text[left] == text[position]) return vertex;
vertex = nodes[vertex].link;
}
}
public:
explicit PalindromicTree(const string& text) {
nodes.reserve(text.size() + 2);
// Each branching node has at least two outgoing edges, and there
// are at most |text| edges in total.
branching.reserve(text.size() / 2 + 1);
nodes.emplace_back(-1, 0); // Imaginary root.
nodes.emplace_back(0, 0); // Empty palindrome.
int last = 1;
for (int i = 0; i < static_cast<int>(text.size()); ++i) {
int letter = text[i] - 'A';
int parent = extendableSuffix(last, i, text);
int child = next(parent, letter);
if (child == 0) {
child = static_cast<int>(nodes.size());
nodes.emplace_back(nodes[parent].length + 2, letter);
addEdge(parent, letter, child);
if (nodes[child].length == 1) {
nodes[child].link = 1;
} else {
int suffix = extendableSuffix(nodes[parent].link, i, text);
nodes[child].link = next(suffix, letter);
}
}
last = child;
++nodes[last].countS;
}
}
long long countPairs(const string& text) {
int last = 1;
for (int i = 0; i < static_cast<int>(text.size()); ++i) {
int letter = text[i] - 'A';
while (true) {
int left = i - nodes[last].length - 1;
if (left >= 0 && text[left] == text[i]) {
int child = next(last, letter);
if (child != 0) {
last = child;
break;
}
}
if (last == 0) {
last = 1; // This letter does not occur in the first string.
break;
}
last = nodes[last].link;
}
if (last >= 2) ++nodes[last].countT;
}
long long answer = 0;
for (int vertex = static_cast<int>(nodes.size()) - 1; vertex >= 2; --vertex) {
answer += 1LL * nodes[vertex].countS * nodes[vertex].countT;
int suffix = nodes[vertex].link;
nodes[suffix].countS += nodes[vertex].countS;
nodes[suffix].countT += nodes[vertex].countT;
}
return answer;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S, T;
cin >> S >> T;
if (S.size() > T.size()) swap(S, T);
PalindromicTree tree(S);
cout << tree.countPairs(T) << '\n';
return 0;
}