답안 #113104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
113104 2019-05-23T16:37:24 Z Anai 회문 (APIO14_palindrome) C++14
100 / 100
79 ms 46460 KB
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

struct PalTree {
	struct Nod {
		map<char, int> tr;
		int suf, aps, l, r;

		inline int len() {
			return r - l + 1; }

 		Nod(int _l = -1, int _r = -1) {
			l = _l, r = _r;
			suf = -1;
			aps = 0; } };

	vector<Nod> pom;
	string str;
	int now;

	PalTree() {
		now = 0;
		pom.emplace_back();
		pom.emplace_back();
		pom[0].suf = pom[0].aps = 0, pom[0].r = -2, pom[0].l = 0; /* imaginary node */
		pom[1].suf = pom[1].aps = 0, pom[1].r = -1, pom[1].l = 0; /* null node */ }

	void push_back(char ch) {
		str.push_back(ch);

		while (now != 0) {
			if (pom[now].len() + 2 <= str.size() && str[str.size() - pom[now].len() - 2] == ch)
				break;
			now = pom[now].suf; }

		int &mynode = pom[now].tr[ch];
		if (mynode != 0) {
			now = mynode;
			pom[now].aps+= 1;
			return; }

		mynode = pom.size();
		pom.emplace_back();
		pom.back().l = str.size() - pom[now].len() - 2;
		pom.back().r = str.size() - 1;
		pom.back().aps = 1;

		int fall = pom[now].suf;
		now = mynode;

		if (pom[now].len() == 1) {
			pom[now].suf = 1;
			return; }

		while (fall != 0) {
			if (str[str.size() - pom[fall].len() - 2] == ch)
				break;
			fall = pom[fall].suf; }
		pom[now].suf = pom[fall].tr[ch]; }
	
	i64 get_ans() {
		i64 ans = 0;
		for (int i = pom.size() - 1; i > 1; --i) {
			pom[pom[i].suf].aps+= pom[i].aps;
			ans = max(ans, 1LL * pom[i].aps * pom[i].len()); }
		return ans; } };

int main() {
#ifdef HOME
	freopen("palindrome.in", "r", stdin);
	freopen("palindrome.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	string str;

	PalTree *pom = new PalTree();

	cin >> str;
	for (const auto &ch: str)
		pom->push_back(ch);
	cout << pom->get_ans() << endl;

	return 0; }

Compilation message

palindrome.cpp: In member function 'void PalTree::push_back(char)':
palindrome.cpp:34:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (pom[now].len() + 2 <= str.size() && str[str.size() - pom[now].len() - 2] == ch)
        ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1760 KB Output is correct
2 Correct 4 ms 1760 KB Output is correct
3 Correct 4 ms 1760 KB Output is correct
4 Correct 5 ms 1760 KB Output is correct
5 Correct 4 ms 1760 KB Output is correct
6 Correct 5 ms 1760 KB Output is correct
7 Correct 4 ms 1760 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 1124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 11876 KB Output is correct
2 Correct 21 ms 11876 KB Output is correct
3 Correct 21 ms 11868 KB Output is correct
4 Correct 24 ms 11992 KB Output is correct
5 Correct 24 ms 11844 KB Output is correct
6 Correct 18 ms 12004 KB Output is correct
7 Correct 21 ms 11996 KB Output is correct
8 Correct 6 ms 896 KB Output is correct
9 Correct 10 ms 3448 KB Output is correct
10 Correct 19 ms 11876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 46304 KB Output is correct
2 Correct 79 ms 46460 KB Output is correct
3 Correct 71 ms 46408 KB Output is correct
4 Correct 73 ms 46424 KB Output is correct
5 Correct 73 ms 46428 KB Output is correct
6 Correct 69 ms 46424 KB Output is correct
7 Correct 55 ms 28660 KB Output is correct
8 Correct 15 ms 1928 KB Output is correct
9 Correct 16 ms 1928 KB Output is correct
10 Correct 52 ms 28132 KB Output is correct
11 Correct 74 ms 46288 KB Output is correct
12 Correct 17 ms 4448 KB Output is correct