結果
| 問題 |
No.2606 Mirror Relay
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-01-12 21:34:37 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 2,000 ms |
| コード長 | 4,448 bytes |
| コンパイル時間 | 1,245 ms |
| コンパイル使用メモリ | 118,624 KB |
| 実行使用メモリ | 28,712 KB |
| 最終ジャッジ日時 | 2024-09-27 21:28:21 |
| 合計ジャッジ時間 | 4,107 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 69 |
ソースコード
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// alphabet is [OFFSET, OFFSET + SIZE), with sentinel (OFFSET - 1)
template <class T, int SIZE, T OFFSET> struct Pam {
struct Node {
// ts[pos, pos + len]: suffix when created
// fail: longest proper palindromic suffix
int len, pos, fail;
int nxt[SIZE];
};
// nodes[0]: length 0 (also means null in nxt)
// nodes[1]: length -1
// nodes[2, nodesLen): non-empty palindromic substring
// suf: node for longest palindromic suffix
int nodesLen, suf;
vector<Node> nodes;
// current whole string: ts[0, r) (0 <= r <= nR)
int nR, r;
vector<T> tsBuffer;
T *ts;
Pam(int nR_) : nR(nR_) {
nodesLen = 2;
suf = 0;
nodes.resize(2 + nR);
nodes[0].len = 0; nodes[0].pos = 0; nodes[0].fail = 1;
nodes[1].len = -1; nodes[1].pos = 0; nodes[1].fail = 1;
memset(nodes[0].nxt, 0, sizeof(nodes[0].nxt));
memset(nodes[1].nxt, 0, sizeof(nodes[1].nxt));
r = 0;
tsBuffer.assign(1 + nR, OFFSET - 1);
ts = tsBuffer.data() + 1;
}
const Node &operator[](int u) const {
return nodes[u];
}
void pushBack(T t) {
assert(r < nR);
const int a = t - OFFSET;
ts[r++] = t;
for (; ts[r - 2 - nodes[suf].len] != t; suf = nodes[suf].fail) {}
Node &f = nodes[suf];
if (!f.nxt[a]) {
Node &g = nodes[nodesLen];
g.len = f.len + 2; g.pos = r - g.len;
int u = f.fail;
for (; ts[r - 2 - nodes[u].len] != t; u = nodes[u].fail) {}
g.fail = nodes[u].nxt[a];
memset(g.nxt, 0, sizeof(g.nxt));
f.nxt[a] = nodesLen++; // this needs to be after setting g.fail
}
suf = f.nxt[a];
}
void dfsPrint(ostream &os, int u, const string &branch, int type) const {
const Node &f = nodes[u];
os << branch << ((type == 0) ? "" : (type == 1) ? "|-- " : "`-- ");
if (f.len <= 0) {
os << "(" << f.len << ")";
} else {
for (int i = f.pos; i < f.pos + f.len; ++i) os << ts[i];
}
os << " " << u << " " << f.fail;
// debug here
os << "\n";
int a0 = -1;
for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) a0 = a;
for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) {
dfsPrint(os, f.nxt[a],
branch + ((type == 0) ? "" : (type == 1) ? "| " : " "),
(a == a0) ? 2 : 1);
}
}
friend ostream &operator<<(ostream &os, const Pam &pam) {
pam.dfsPrint(os, 0, " ", 0);
pam.dfsPrint(os, 1, "", 0);
return os;
}
};
int N;
char S[200'010];
int main() {
for (; ~scanf("%s", S); ) {
N = strlen(S);
Pam<char, 26, 'a'> pam(N);
vector<int> us(N + 1);
us[0] = 0;
for (int i = 0; i < N; ++i) {
pam.pushBack(S[i]);
us[i + 1] = pam.suf;
}
// cerr<<pam<<"us = "<<us<<endl;
vector<Int> dp(pam.nodesLen, 0);
for (int i = 1; i <= N; ++i) ++dp[us[i]];
// cerr<<"dp = "<<dp<<endl;
for (int u = pam.nodesLen; --u >= 2; ) dp[pam.nodes[u].fail] += dp[u];
// cerr<<"dp = "<<dp<<endl;
for (int u = 0; u < pam.nodesLen; ++u) dp[u] *= pam.nodes[u].len;
for (int u = 2; u < pam.nodesLen; ++u) dp[u] += dp[pam.nodes[u].fail];
// cerr<<"dp = "<<dp<<endl;
Int ans = 0;
for (int u = 0; u < pam.nodesLen; ++u) chmax(ans, dp[u]);
printf("%lld\n", ans);
}
return 0;
}