結果
| 問題 |
No.2606 Mirror Relay
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-04-13 07:50:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 83 ms / 2,000 ms |
| コード長 | 2,530 bytes |
| コンパイル時間 | 1,134 ms |
| コンパイル使用メモリ | 92,704 KB |
| 実行使用メモリ | 50,980 KB |
| 最終ジャッジ日時 | 2024-10-02 23:56:48 |
| 合計ジャッジ時間 | 2,954 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 69 |
ソースコード
#include <iostream>
#include <vector>
#include <unordered_map>
#define int long long
using namespace std;
class PalindromicTree {
private:
vector<char> st;
vector<int> length;
vector<int> parent;
vector<int> count;
vector<unordered_map<char, int>> link;
int lastNodeIdx;
public:
PalindromicTree() {
st.push_back(0);
length.push_back(-1);
length.push_back(0);
parent.push_back(0);
parent.push_back(0);
count.push_back(0);
count.push_back(0);
link.push_back({});
link.push_back({});
lastNodeIdx = 0;
}
void add(char v) {
st.push_back(v);
int prevNodeIdx = lastNodeIdx;
int nextNodeIdx = lastNodeIdx;
while (true) {
int head = length[nextNodeIdx] + 2;
if (head <= st.size() && st[st.size() - head] == st.back()) {
break;
}
nextNodeIdx = parent[nextNodeIdx];
}
int nextLen = length[nextNodeIdx] + 2;
if (link[nextNodeIdx].count(v)) {
lastNodeIdx = link[nextNodeIdx][v];
count[lastNodeIdx]++;
return;
}
lastNodeIdx = length.size();
length.push_back(nextLen);
if (nextLen == 1) {
parent.push_back(1);
} else {
int prevNodeIdx = parent[nextNodeIdx];
while (true) {
int head = length[prevNodeIdx] + 2;
if (head <= st.size() && st[st.size() - head] == st.back()) {
break;
}
prevNodeIdx = parent[prevNodeIdx];
}
parent.push_back(link[prevNodeIdx][v]);
}
count.push_back(1);
link[nextNodeIdx][v] = lastNodeIdx;
link.push_back({});
}
int maxScore() {
int nodeCount = length.size();
vector<int> score(nodeCount, 0);
vector<int> tempCount = count;
for (int i = nodeCount - 1; i >= 2; i--) {
tempCount[parent[i]] += tempCount[i];
}
for (int i = 2; i < nodeCount; i++) {
score[i] = score[parent[i]] + length[i] * tempCount[i];
}
int maxScore = 0;
for (int s : score) {
maxScore = max(maxScore, s);
}
return maxScore;
}
};
signed main() {
string s;
cin >> s;
PalindromicTree pt;
for (int i = s.length() - 1; i >= 0; i--) {
pt.add(s[i]);
}
cout << pt.maxScore() << endl;
return 0;
}