Submission #400548

#TimeUsernameProblemLanguageResultExecution timeMemory
400548BERNARB01Palindromes (APIO14_palindrome)C++17
0 / 100
33 ms38424 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
template<const int A>
class eertree {
private:
	int n, id, cr;
	vector<array<int, A>> T;
	vector<int> P, L, C;
	void add(const string& s, int i) {
		int ch = s[i] - 'a';
		while (i - L[cr] - 1 < 0 || s[i - L[cr] - 1] != s[i]) {
			cr = P[cr];
		}
		if (T[cr][ch] == 0) {
			T[cr][ch] = id++;
		}
		int new_cr = T[cr][ch];
		L[new_cr] = 2 + L[cr];
		C[new_cr]++;
		if (L[new_cr] == 1) {
			P[new_cr] = 1;
		} else if (!P[new_cr]) {
			cr = P[cr];
			while (!T[cr][ch]) {
				cr = P[cr];
			}
			P[new_cr] = T[cr][ch];
		}
		cr = new_cr;
	}
public:
	eertree() {};
	eertree(const string& s) {
		n = s.length();
		T.assign(n + 2, {});
		P.assign(n + 2, 0);
		L.assign(n + 2, 0);
		C.assign(n + 2, 0);
		L[0] = -1;
		id = 2;
		cr = 0;
		for (int i = 0; i < n; i++) {
			add(s, i);
		}
	}
	long long solve() {
		vector<int> inDeg(n + 2, 0);
		for (int i = 2; i < n + 2; i++) {
			inDeg[P[i]]++;
		}
		vector<int> que;
		for (int i = 2; i < n + 2; i++) {
			if (inDeg[i] == 0) {
				que.push_back(i);
			}
		}
		for (int b = 0; b < (int) que.size(); b++) {
			int u = que[b];
			int v = P[u];
			if (v < 2) {
				continue;
			}
			C[v] += C[u];
			inDeg[v]--;
			if (inDeg[v] == 0) {
				que.push_back(v);
			}
		}
		long long res = 0;
		for (int i = 2; i < n + 2; i++) {
			res = max(res, C[i] * 1LL * L[i]);
		}
		return res;
	}
};
 
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	string s;
	cin >> s;
	eertree<26> pals(s);
	cout << pals.solve() << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...