답안 #921430

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
921430 2024-02-03T20:19:34 Z AVIJIT_BISWAS 회문 (APIO14_palindrome) C++14
100 / 100
72 ms 73956 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 10 ms 45404 KB Output is correct
2 Correct 9 ms 45404 KB Output is correct
3 Correct 10 ms 45404 KB Output is correct
4 Correct 9 ms 45400 KB Output is correct
5 Correct 10 ms 45404 KB Output is correct
6 Correct 9 ms 45404 KB Output is correct
7 Correct 9 ms 45400 KB Output is correct
8 Correct 9 ms 45656 KB Output is correct
9 Correct 9 ms 45520 KB Output is correct
10 Correct 9 ms 45400 KB Output is correct
11 Correct 9 ms 45400 KB Output is correct
12 Correct 9 ms 45404 KB Output is correct
13 Correct 11 ms 45404 KB Output is correct
14 Correct 10 ms 45404 KB Output is correct
15 Correct 9 ms 45400 KB Output is correct
16 Correct 9 ms 45404 KB Output is correct
17 Correct 9 ms 45520 KB Output is correct
18 Correct 9 ms 45656 KB Output is correct
19 Correct 9 ms 45404 KB Output is correct
20 Correct 9 ms 45412 KB Output is correct
21 Correct 10 ms 45404 KB Output is correct
22 Correct 9 ms 45404 KB Output is correct
23 Correct 9 ms 45404 KB Output is correct
24 Correct 9 ms 45528 KB Output is correct
25 Correct 10 ms 45404 KB Output is correct
26 Correct 9 ms 45404 KB Output is correct
27 Correct 9 ms 45404 KB Output is correct
28 Correct 9 ms 45524 KB Output is correct
29 Correct 9 ms 45404 KB Output is correct
30 Correct 9 ms 45404 KB Output is correct
31 Correct 9 ms 45404 KB Output is correct
32 Correct 10 ms 45432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 45404 KB Output is correct
2 Correct 9 ms 45404 KB Output is correct
3 Correct 9 ms 45400 KB Output is correct
4 Correct 9 ms 45400 KB Output is correct
5 Correct 9 ms 45404 KB Output is correct
6 Correct 10 ms 45400 KB Output is correct
7 Correct 9 ms 45400 KB Output is correct
8 Correct 9 ms 45404 KB Output is correct
9 Correct 8 ms 45404 KB Output is correct
10 Correct 9 ms 45488 KB Output is correct
11 Correct 9 ms 45404 KB Output is correct
12 Correct 9 ms 45404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 45916 KB Output is correct
2 Correct 10 ms 46068 KB Output is correct
3 Correct 11 ms 46468 KB Output is correct
4 Correct 11 ms 46172 KB Output is correct
5 Correct 10 ms 45660 KB Output is correct
6 Correct 10 ms 45732 KB Output is correct
7 Correct 12 ms 45916 KB Output is correct
8 Correct 10 ms 45404 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 24 ms 51804 KB Output is correct
2 Correct 23 ms 50264 KB Output is correct
3 Correct 26 ms 55012 KB Output is correct
4 Correct 23 ms 52572 KB Output is correct
5 Correct 19 ms 46684 KB Output is correct
6 Correct 18 ms 47732 KB Output is correct
7 Correct 23 ms 48728 KB Output is correct
8 Correct 11 ms 45660 KB Output is correct
9 Correct 15 ms 47660 KB Output is correct
10 Correct 16 ms 46684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 64484 KB Output is correct
2 Correct 46 ms 57828 KB Output is correct
3 Correct 57 ms 73956 KB Output is correct
4 Correct 53 ms 61832 KB Output is correct
5 Correct 72 ms 50052 KB Output is correct
6 Correct 43 ms 56796 KB Output is correct
7 Correct 43 ms 54748 KB Output is correct
8 Correct 14 ms 46048 KB Output is correct
9 Correct 14 ms 46308 KB Output is correct
10 Correct 52 ms 49636 KB Output is correct
11 Correct 48 ms 58588 KB Output is correct
12 Correct 18 ms 48560 KB Output is correct