답안 #265515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
265515 2020-08-14T23:22:34 Z square1001 회문 (APIO14_palindrome) C++14
47 / 100
1000 ms 18212 KB
// APIO 2014 Problem 1 - Palindromes

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	// step #1. read input
	string S;
	cin >> S;
	int N = S.size();
	// step #2. construct suffix array of T = S + "#" + rev(S)
	string RS = S;
	reverse(RS.begin(), RS.end());
	string T = S + "#" + RS;
	vector<int> sa_inv(2 * N + 1);
	for(int i = 0; i < 2 * N + 1; ++i) {
		sa_inv[i] = int(T[i]);
	}
	for(int i = 1; i < 2 * N + 1; i *= 2) {
		vector<pair<int, int> > nseq(2 * N + 1);
		for(int j = 0; j < 2 * N + 1; ++j) {
			nseq[j] = make_pair(sa_inv[j], j + i < 2 * N + 1 ? sa_inv[j + i] : -1);
		}
		vector<pair<int, int> > sseq(nseq);
		sort(sseq.begin(), sseq.end());
		sseq.erase(unique(sseq.begin(), sseq.end()), sseq.end());
		for(int j = 0; j < 2 * N + 1; ++j) {
			sa_inv[j] = lower_bound(sseq.begin(), sseq.end(), nseq[j]) - sseq.begin();
		}
	}
	vector<int> sa(2 * N + 1);
	for(int i = 0; i < 2 * N + 1; ++i) {
		sa[sa_inv[i]] = i;
	}
	// step #3. construct rolling-hash table and rolling-hash function
	const int mod = 469762049;
	const int base = 311;
	vector<int> pw(2 * N + 2), h(2 * N + 2);
	pw[0] = 1;
	for(int i = 0; i < 2 * N + 1; ++i) {
		pw[i + 1] = 1LL * pw[i] * base % mod;
		h[i + 1] = (1LL * h[i] * base + T[i]) % mod;
	}
	function<int(int, int)> gethash = [&](int l, int r) {
		return (h[r] - 1LL * h[l] * pw[r - l] % mod + mod) % mod;
	};
	// step #4. calculate LCP
	vector<int> lcp(2 * N);
	for(int i = 0; i < 2 * N; ++i) {
		int l = 0, r = (2 * N + 1) - max(sa[i], sa[i + 1]) + 1;
		while(r - l > 1) {
			int m = (l + r) >> 1;
			if(gethash(sa[i], sa[i] + m) == gethash(sa[i + 1], sa[i + 1] + m)) l = m;
			else r = m;
		}
		lcp[i] = l;
	}
	// step #5. calculate types and lengths of elements in suffix array
	vector<pair<int, int> > trait(2 * N + 1);
	for(int i = 0; i < 2 * N + 1; ++i) {
		if(sa[i] == N) trait[i] = make_pair(0, -1);
		else if(sa[i] < N) trait[i] = make_pair(1, N - sa[i]);
		else trait[i] = make_pair(2, (2 * N + 1) - sa[i]);
	}
	// step #6. calculate the answer
	vector<bool> flag(N + 1, false);
	function<long long(int, int)> solve = [&](int l, int r) {
		int baselen = trait[l].second;
		for(int i = l; i < r - 1; ++i) {
			baselen = min(baselen, lcp[i]);
		}
		for(int i = l; i < r; ++i) {
			if(trait[i].first == 1) {
				flag[trait[i].second] = true;
			}
		}
		int ct1 = 0, ct2 = 0;
		for(int i = l; i < r; ++i) {
			if(trait[i].first == 2) {
				if(flag[N - trait[i].second]) ++ct1;
				if(flag[N - trait[i].second + 1]) ++ct2;
			}
		}
		for(int i = l; i < r; ++i) {
			if(trait[i].first == 1) {
				flag[trait[i].second] = false;
			}
		}
		long long ans = 0;
		ans = max(ans, 1LL * (baselen * 2) * ct1);
		ans = max(ans, 1LL * (baselen * 2 - 1) * ct2);
		if(r - l > 1) {
			int pre = l;
			for(int i = l; i < r; ++i) {
				if(i == r - 1 || lcp[i] == baselen) {
					long long res = solve(pre, i + 1);
					ans = max(ans, res);
					pre = i + 1;
				}
			}
		}
		return ans;
	};
	long long ans = solve(1, 2 * N + 1);
	// step #7. print the answer
	cout << ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 256 KB Output is correct
22 Correct 1 ms 256 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 256 KB Output is correct
27 Correct 1 ms 256 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 1 ms 256 KB Output is correct
31 Correct 1 ms 256 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 512 KB Output is correct
2 Correct 7 ms 384 KB Output is correct
3 Correct 15 ms 512 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 11 ms 512 KB Output is correct
6 Correct 11 ms 512 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 14 ms 512 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 907 ms 2316 KB Output is correct
2 Correct 328 ms 1436 KB Output is correct
3 Correct 947 ms 2300 KB Output is correct
4 Correct 691 ms 2204 KB Output is correct
5 Correct 51 ms 924 KB Output is correct
6 Correct 75 ms 1052 KB Output is correct
7 Correct 207 ms 1312 KB Output is correct
8 Correct 68 ms 1052 KB Output is correct
9 Correct 69 ms 888 KB Output is correct
10 Correct 49 ms 924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1086 ms 6676 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1094 ms 18212 KB Time limit exceeded
2 Halted 0 ms 0 KB -