답안 #958515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958515 2024-04-06T00:14:54 Z MinaRagy06 회문 (APIO14_palindrome) C++17
100 / 100
17 ms 36836 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 300'005;
struct node {
	int nxt[26];
	int par, len, suflink, cnt;
	node() {
		memset(nxt, 0, sizeof nxt);
		par = len = suflink = cnt = 0;
	}
} tree[N];
int nodes = 0, suf;
void build() {
	nodes++;
	tree[nodes].len = -1;
	tree[nodes].suflink = nodes;
	nodes++;
	tree[nodes].len = 0;
	tree[nodes].suflink = nodes - 1;
}
ll ans = 0;
int dfs(int i) {
	int cnt = tree[i].cnt;
	for (int j = 0; j < 26; j++) {
		if (!tree[i].nxt[j]) continue;
		cnt += dfs(tree[i].nxt[j]);
	}
	ans = max(ans, 1ll * cnt * tree[i].len);
	return cnt;
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	string s;
	cin >> s;
	int n = s.size();
	build();
	suf = 1;
	for (int i = 0; i < n; i++) {
		int cur = suf;
		while (i - tree[cur].len - 1 < 0 || s[i - tree[cur].len - 1] != s[i]) {
			cur = tree[cur].suflink;
		}
		bool old = 1;
		if (!tree[cur].nxt[s[i] - 'a']) {
			old = 0;
			nodes++;
			tree[cur].nxt[s[i] - 'a'] = nodes;
			tree[nodes].len = tree[cur].len + 2;
			tree[nodes].par = cur;
		}
		int p = tree[cur].nxt[s[i] - 'a'];
		tree[p].cnt++;
		suf = p;
		if (old) continue;
		if (tree[p].len == 1) {
			tree[p].suflink = 2;
			continue;
		}
		cur = tree[cur].suflink;
		while (i - tree[cur].len - 1 < 0 || s[i - tree[cur].len - 1] != s[i]) {
			cur = tree[cur].suflink;
		}
		cur = tree[cur].nxt[s[i] - 'a'];
		tree[p].suflink = cur;
	}
	for (int i = nodes; i >= 1; i--) {
// 		tree[tree[i].par].cnt += tree[i].cnt;
		tree[tree[i].suflink].cnt += tree[i].cnt;
// 		cout << i << ": " << tree[i].len << ' ' << tree[i].cnt << ' ' << tree[i].par << ' ' << tree[i].suflink << '\n';
		ans = max(ans, 1ll * tree[i].cnt * tree[i].len);
	}
	cout << ans << '\n';
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35676 KB Output is correct
3 Correct 7 ms 35676 KB Output is correct
4 Correct 7 ms 35672 KB Output is correct
5 Correct 7 ms 35488 KB Output is correct
6 Correct 8 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35676 KB Output is correct
9 Correct 8 ms 35676 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
11 Correct 8 ms 35680 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
13 Correct 8 ms 35672 KB Output is correct
14 Correct 8 ms 35676 KB Output is correct
15 Correct 8 ms 35672 KB Output is correct
16 Correct 8 ms 35676 KB Output is correct
17 Correct 8 ms 35448 KB Output is correct
18 Correct 7 ms 35468 KB Output is correct
19 Correct 8 ms 35676 KB Output is correct
20 Correct 8 ms 35684 KB Output is correct
21 Correct 8 ms 35676 KB Output is correct
22 Correct 8 ms 35676 KB Output is correct
23 Correct 8 ms 35676 KB Output is correct
24 Correct 8 ms 35676 KB Output is correct
25 Correct 8 ms 35676 KB Output is correct
26 Correct 8 ms 35472 KB Output is correct
27 Correct 8 ms 35676 KB Output is correct
28 Correct 9 ms 35676 KB Output is correct
29 Correct 8 ms 35676 KB Output is correct
30 Correct 8 ms 35928 KB Output is correct
31 Correct 8 ms 35468 KB Output is correct
32 Correct 8 ms 35672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35676 KB Output is correct
2 Correct 8 ms 35676 KB Output is correct
3 Correct 8 ms 35684 KB Output is correct
4 Correct 9 ms 35676 KB Output is correct
5 Correct 8 ms 35676 KB Output is correct
6 Correct 8 ms 35688 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 7 ms 35440 KB Output is correct
9 Correct 8 ms 35672 KB Output is correct
10 Correct 8 ms 35672 KB Output is correct
11 Correct 8 ms 35672 KB Output is correct
12 Correct 8 ms 35676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 35672 KB Output is correct
2 Correct 8 ms 35676 KB Output is correct
3 Correct 8 ms 35676 KB Output is correct
4 Correct 8 ms 35676 KB Output is correct
5 Correct 10 ms 35676 KB Output is correct
6 Correct 8 ms 35676 KB Output is correct
7 Correct 8 ms 35676 KB Output is correct
8 Correct 8 ms 35676 KB Output is correct
9 Correct 9 ms 35720 KB Output is correct
10 Correct 8 ms 35676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 35932 KB Output is correct
2 Correct 9 ms 35928 KB Output is correct
3 Correct 11 ms 35932 KB Output is correct
4 Correct 11 ms 35932 KB Output is correct
5 Correct 11 ms 35932 KB Output is correct
6 Correct 9 ms 35928 KB Output is correct
7 Correct 10 ms 35932 KB Output is correct
8 Correct 10 ms 35932 KB Output is correct
9 Correct 9 ms 35932 KB Output is correct
10 Correct 9 ms 35804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 36836 KB Output is correct
2 Correct 15 ms 36580 KB Output is correct
3 Correct 15 ms 36580 KB Output is correct
4 Correct 16 ms 36324 KB Output is correct
5 Correct 16 ms 36576 KB Output is correct
6 Correct 15 ms 36580 KB Output is correct
7 Correct 16 ms 36592 KB Output is correct
8 Correct 12 ms 36576 KB Output is correct
9 Correct 13 ms 36628 KB Output is correct
10 Correct 14 ms 36580 KB Output is correct
11 Correct 17 ms 36576 KB Output is correct
12 Correct 12 ms 36580 KB Output is correct