답안 #959974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
959974 2024-04-09T11:32:03 Z KasymK 회문 (APIO14_palindrome) C++17
100 / 100
60 ms 74080 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

struct Node {
	int nxt[26], sufflink;
	ll len, cnt;
	vector<int> edges;
} tree[303030];

string s;
int suff, num;
ll ans = 0;

void add_letter(int pos) {
	int curr = suff, curr_len = 0;
	int letter = s[pos] - 'a';

	while (true) {
		curr_len = tree[curr].len;
		if (pos - 1 - curr_len > -1 && s[pos - 1 - curr_len] == s[pos]) break;
		curr = tree[curr].sufflink;
	}
	if (tree[curr].nxt[letter]) {
		suff = tree[curr].nxt[letter];
		tree[suff].cnt++;
		return;
	}

	suff = ++num;
	tree[num].len = tree[curr].len + 2;
	tree[num].cnt = 1;
	tree[curr].nxt[letter] = num;

	if (tree[num].len == 1) {
		tree[num].sufflink = 2;
		tree[2].edges.push_back(num);
		return;
	}

	while (true) {
		curr = tree[curr].sufflink;
		curr_len = tree[curr].len;
		if (pos - 1 - curr_len > -1 && s[pos - 1 - curr_len] == s[pos]) {
			tree[num].sufflink = tree[curr].nxt[letter];
			tree[tree[curr].nxt[letter]].edges.push_back(num);
			break;
		}
	}
}

void init() {
	num = 2, suff = 2;
	tree[1].len = -1, tree[1].sufflink = 1;
	tree[2].len = 0, tree[2].sufflink = 1;
	tree[1].edges.push_back(2);
}

void dfs(int node = 1) {
	for (int i : tree[node].edges) {
		dfs(i);
		tree[node].cnt += tree[i].cnt;
	}
	ans = max(ans, tree[node].len * tree[node].cnt);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> s;
	init();
	FOR(i, 0, s.size()) add_letter(i);
	dfs();
	cout << ans;
	return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:2:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define FOR(i, x, y) for (int i = x; i < y; i++)
......
   73 |  FOR(i, 0, s.size()) add_letter(i);
      |      ~~~~~~~~~~~~~~                     
palindrome.cpp:73:2: note: in expansion of macro 'FOR'
   73 |  FOR(i, 0, s.size()) add_letter(i);
      |  ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 45404 KB Output is correct
2 Correct 9 ms 45404 KB Output is correct
3 Correct 8 ms 45528 KB Output is correct
4 Correct 8 ms 45404 KB Output is correct
5 Correct 9 ms 45404 KB Output is correct
6 Correct 9 ms 45524 KB Output is correct
7 Correct 8 ms 45404 KB Output is correct
8 Correct 8 ms 45404 KB Output is correct
9 Correct 9 ms 45404 KB Output is correct
10 Correct 10 ms 45484 KB Output is correct
11 Correct 9 ms 45404 KB Output is correct
12 Correct 9 ms 45484 KB Output is correct
13 Correct 8 ms 45404 KB Output is correct
14 Correct 9 ms 45404 KB Output is correct
15 Correct 8 ms 45400 KB Output is correct
16 Correct 11 ms 45400 KB Output is correct
17 Correct 8 ms 45480 KB Output is correct
18 Correct 9 ms 45404 KB Output is correct
19 Correct 10 ms 45648 KB Output is correct
20 Correct 8 ms 45400 KB Output is correct
21 Correct 9 ms 45400 KB Output is correct
22 Correct 10 ms 45528 KB Output is correct
23 Correct 8 ms 45656 KB Output is correct
24 Correct 8 ms 45400 KB Output is correct
25 Correct 8 ms 45404 KB Output is correct
26 Correct 9 ms 45400 KB Output is correct
27 Correct 9 ms 45400 KB Output is correct
28 Correct 10 ms 45916 KB Output is correct
29 Correct 8 ms 45404 KB Output is correct
30 Correct 8 ms 45400 KB Output is correct
31 Correct 8 ms 45400 KB Output is correct
32 Correct 8 ms 45400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 45400 KB Output is correct
2 Correct 9 ms 45476 KB Output is correct
3 Correct 10 ms 45404 KB Output is correct
4 Correct 9 ms 45404 KB Output is correct
5 Correct 10 ms 45404 KB Output is correct
6 Correct 9 ms 45400 KB Output is correct
7 Correct 9 ms 45400 KB Output is correct
8 Correct 8 ms 45400 KB Output is correct
9 Correct 8 ms 45404 KB Output is correct
10 Correct 9 ms 45404 KB Output is correct
11 Correct 9 ms 45404 KB Output is correct
12 Correct 9 ms 45544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 45912 KB Output is correct
2 Correct 9 ms 45916 KB Output is correct
3 Correct 10 ms 46444 KB Output is correct
4 Correct 10 ms 46384 KB Output is correct
5 Correct 9 ms 45660 KB Output is correct
6 Correct 9 ms 45660 KB Output is correct
7 Correct 9 ms 45916 KB Output is correct
8 Correct 8 ms 45400 KB Output is correct
9 Correct 9 ms 45404 KB Output is correct
10 Correct 9 ms 45404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 51804 KB Output is correct
2 Correct 24 ms 50520 KB Output is correct
3 Correct 25 ms 54880 KB Output is correct
4 Correct 21 ms 53080 KB Output is correct
5 Correct 18 ms 46680 KB Output is correct
6 Correct 15 ms 47704 KB Output is correct
7 Correct 18 ms 48988 KB Output is correct
8 Correct 10 ms 45660 KB Output is correct
9 Correct 12 ms 47708 KB Output is correct
10 Correct 15 ms 46672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 64732 KB Output is correct
2 Correct 41 ms 58072 KB Output is correct
3 Correct 57 ms 74080 KB Output is correct
4 Correct 43 ms 62112 KB Output is correct
5 Correct 60 ms 50404 KB Output is correct
6 Correct 41 ms 57304 KB Output is correct
7 Correct 37 ms 54944 KB Output is correct
8 Correct 14 ms 46304 KB Output is correct
9 Correct 13 ms 46308 KB Output is correct
10 Correct 44 ms 49632 KB Output is correct
11 Correct 43 ms 58472 KB Output is correct
12 Correct 16 ms 48612 KB Output is correct