Submission #969983

# Submission time Handle Problem Language Result Execution time Memory
969983 2024-04-26T03:03:15 Z TranGiaHuy1508 Palindromes (APIO14_palindrome) C++17
100 / 100
57 ms 66152 KB
#include <bits/stdc++.h>
using namespace std;

void main_program();

signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	main_program();
}

template <class T, class Adj>
struct PalindromeTree {
	vector<int> len, link, occur;
	vector<Adj> nxt;

	PalindromeTree(){
		len = vector<int>{0, -1};
		link = vector<int>{1, 0};
		occur = vector<int>{1, 0};
		nxt = vector<Adj>(2);
	}
	PalindromeTree(const vector<T> &str): PalindromeTree() {
		add(str);
	}

	int new_node(int length, int sfx_link){
		len.push_back(length);
		link.push_back(sfx_link);
		nxt.emplace_back();
		occur.push_back(0);
		return (int)len.size() - 1;
	}

	vector<T> s = {-1};
	int last = 0;

	void add(T c){
		s.push_back(c);
		int crr = last;
		while (s[(int)s.size() - len[crr] - 2] != c) crr = link[crr];
		if (!nxt[crr][c]){
			int u = link[crr];
			while (s[(int)s.size() - len[u] - 2] != c) u = link[u];
			int v = new_node(len[crr] + 2, nxt[u][c]);
			nxt[crr][c] = v;
		}
		last = nxt[crr][c];
		occur[last]++;
	}
	void add(const vector<T> &str){
		for (auto c: str) add(c);
	}

	vector<int> cnt;
	vector<vector<int>> inv_link;

	void dfs(int x){
		for (auto child: inv_link[x]){
			dfs(child);
			cnt[x] += cnt[child];
		}
	}

	int size() const { return len.size(); }

	void init_count(){
		cnt = occur;
		inv_link.assign(size(), {});

		for (int i = 2; i < size(); i++) inv_link[link[i]].push_back(i);
		dfs(0);
	}
};

void main_program(){
	string s; cin >> s;

	vector<int> v(s.size());
	for (int i = 0; i < (int)s.size(); i++){
		v[i] = s[i] - 'a';
	}

	PalindromeTree<int, array<int, 26>> tree(v);
	tree.init_count();

	long long res = 0;
	for (int i = 2; i < (int)tree.size(); i++){
		res = max(res, 1LL * tree.len[i] * tree.cnt[i]);
	}

	cout << res << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 456 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 856 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 360 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2424 KB Output is correct
2 Correct 2 ms 2420 KB Output is correct
3 Correct 2 ms 2420 KB Output is correct
4 Correct 2 ms 2424 KB Output is correct
5 Correct 2 ms 2236 KB Output is correct
6 Correct 3 ms 2348 KB Output is correct
7 Correct 2 ms 2232 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 1476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 21520 KB Output is correct
2 Correct 15 ms 20156 KB Output is correct
3 Correct 18 ms 22548 KB Output is correct
4 Correct 18 ms 21780 KB Output is correct
5 Correct 15 ms 17684 KB Output is correct
6 Correct 12 ms 16660 KB Output is correct
7 Correct 14 ms 18368 KB Output is correct
8 Correct 2 ms 1752 KB Output is correct
9 Correct 5 ms 5992 KB Output is correct
10 Correct 13 ms 17592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 64384 KB Output is correct
2 Correct 45 ms 63844 KB Output is correct
3 Correct 49 ms 65376 KB Output is correct
4 Correct 44 ms 64872 KB Output is correct
5 Correct 48 ms 62316 KB Output is correct
6 Correct 45 ms 66152 KB Output is correct
7 Correct 40 ms 45908 KB Output is correct
8 Correct 7 ms 4312 KB Output is correct
9 Correct 6 ms 4312 KB Output is correct
10 Correct 34 ms 41572 KB Output is correct
11 Correct 53 ms 64108 KB Output is correct
12 Correct 11 ms 9172 KB Output is correct