답안 #41655

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41655 2018-02-20T06:00:13 Z kingpig9 회문 (APIO14_palindrome) C++11
100 / 100
54 ms 42332 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 300763;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

struct palindromic_tree {
	int slen;	//total length of string
	char str[MAXN];
	int len[MAXN];
	int nnodes;
	int link[MAXN], nxt[MAXN][26];
	int last;
	int cnt[MAXN];	//count of the node
	vector<int> adj[MAXN];

	palindromic_tree() {
		str[0] = -1;
		slen = 1;
		link[0] = 1;
		len[1] = -1;
		nnodes = 2;
	}

	int getlink (int x) {
		while (str[slen - 2 - len[x]] != str[slen - 1]) {
			//debug("str[%d] != str[%d]\n", slen - 2 - len[x], slen - 1);
			x = link[x];
		}
		//debug("finally! str[%d] == str[%d]\n", slen - 2 - len[x], slen - 1);
		return x;
	}

	void add (char ch) {
		ch -= 'a';
		str[slen++] = ch;
		last = getlink(last);
		if (!nxt[last][ch]) {
			len[nnodes] = len[last] + 2;
			link[nnodes] = nxt[getlink(link[last])][ch];	//link with next char added to end
			nxt[last][ch] = nnodes;
			nnodes++;
		}
		last = nxt[last][ch];
	}
} tr;

int N;
char S[MAXN];

int main() {
	scanf("%s", S);
	N = strlen(S);
	for (int i = 0; i < N; i++) {
		tr.add(S[i]);
		tr.cnt[tr.last]++;
	}

	for (int i = tr.nnodes - 1; i > 1; i--) {
		tr.cnt[tr.link[i]] += tr.cnt[i];
	}

	ll ans = 0;
	for (int i = 2; i < tr.nnodes; i++) {
		ans = max(ans, ll(tr.len[i]) * tr.cnt[i]);
	}
	printf("%lld\n", ans);
}

Compilation message

palindrome.cpp: In member function 'void palindromic_tree::add(char)':
palindrome.cpp:45:20: warning: array subscript has type 'char' [-Wchar-subscripts]
   if (!nxt[last][ch]) {
                    ^
palindrome.cpp:47:46: warning: array subscript has type 'char' [-Wchar-subscripts]
    link[nnodes] = nxt[getlink(link[last])][ch]; //link with next char added to end
                                              ^
palindrome.cpp:48:16: warning: array subscript has type 'char' [-Wchar-subscripts]
    nxt[last][ch] = nnodes;
                ^
palindrome.cpp:51:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   last = nxt[last][ch];
                      ^
palindrome.cpp: In function 'int main()':
palindrome.cpp:59:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", S);
                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7592 KB Output is correct
4 Correct 8 ms 7592 KB Output is correct
5 Correct 7 ms 7592 KB Output is correct
6 Correct 7 ms 7596 KB Output is correct
7 Correct 8 ms 7596 KB Output is correct
8 Correct 9 ms 7596 KB Output is correct
9 Correct 7 ms 7648 KB Output is correct
10 Correct 7 ms 7648 KB Output is correct
11 Correct 7 ms 7648 KB Output is correct
12 Correct 8 ms 7648 KB Output is correct
13 Correct 7 ms 7648 KB Output is correct
14 Correct 7 ms 7648 KB Output is correct
15 Correct 7 ms 7648 KB Output is correct
16 Correct 7 ms 7660 KB Output is correct
17 Correct 7 ms 7660 KB Output is correct
18 Correct 9 ms 7660 KB Output is correct
19 Correct 7 ms 7736 KB Output is correct
20 Correct 9 ms 7736 KB Output is correct
21 Correct 9 ms 7736 KB Output is correct
22 Correct 9 ms 7736 KB Output is correct
23 Correct 7 ms 7736 KB Output is correct
24 Correct 9 ms 7736 KB Output is correct
25 Correct 7 ms 7736 KB Output is correct
26 Correct 7 ms 7736 KB Output is correct
27 Correct 7 ms 7736 KB Output is correct
28 Correct 7 ms 7736 KB Output is correct
29 Correct 8 ms 7736 KB Output is correct
30 Correct 9 ms 7736 KB Output is correct
31 Correct 7 ms 7736 KB Output is correct
32 Correct 8 ms 7736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7788 KB Output is correct
2 Correct 7 ms 7792 KB Output is correct
3 Correct 7 ms 7792 KB Output is correct
4 Correct 7 ms 7792 KB Output is correct
5 Correct 7 ms 7792 KB Output is correct
6 Correct 9 ms 7792 KB Output is correct
7 Correct 7 ms 7792 KB Output is correct
8 Correct 7 ms 7792 KB Output is correct
9 Correct 9 ms 7792 KB Output is correct
10 Correct 8 ms 7792 KB Output is correct
11 Correct 7 ms 7792 KB Output is correct
12 Correct 7 ms 7792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 8812 KB Output is correct
2 Correct 11 ms 8812 KB Output is correct
3 Correct 10 ms 8812 KB Output is correct
4 Correct 8 ms 8812 KB Output is correct
5 Correct 11 ms 8812 KB Output is correct
6 Correct 11 ms 8812 KB Output is correct
7 Correct 8 ms 8812 KB Output is correct
8 Correct 7 ms 8812 KB Output is correct
9 Correct 9 ms 8812 KB Output is correct
10 Correct 8 ms 8812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 19180 KB Output is correct
2 Correct 19 ms 19180 KB Output is correct
3 Correct 20 ms 19180 KB Output is correct
4 Correct 19 ms 19180 KB Output is correct
5 Correct 20 ms 19180 KB Output is correct
6 Correct 22 ms 19180 KB Output is correct
7 Correct 23 ms 19180 KB Output is correct
8 Correct 11 ms 19180 KB Output is correct
9 Correct 15 ms 19180 KB Output is correct
10 Correct 19 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 42220 KB Output is correct
2 Correct 46 ms 42332 KB Output is correct
3 Correct 45 ms 42332 KB Output is correct
4 Correct 49 ms 42332 KB Output is correct
5 Correct 48 ms 42332 KB Output is correct
6 Correct 39 ms 42332 KB Output is correct
7 Correct 41 ms 42332 KB Output is correct
8 Correct 14 ms 42332 KB Output is correct
9 Correct 12 ms 42332 KB Output is correct
10 Correct 54 ms 42332 KB Output is correct
11 Correct 42 ms 42332 KB Output is correct
12 Correct 15 ms 42332 KB Output is correct