답안 #879410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879410 2023-11-27T10:27:23 Z marvinthang 회문 (APIO14_palindrome) C++17
100 / 100
292 ms 71520 KB
/*************************************
*    author: marvinthang             *
*    created: 27.11.2023 15:58:18    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i-- > 0; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

// end of template

const int BASE = 31;

void process(void) {
	string s; cin >> s;

	auto manacher_odd = [&] (string s) {
		int n = s.size();
		s = '$' + s + '^';
		vector <int> p(n + 1);
		int l = 1, r = 1;
		FORE(i, 1, n) {
			p[i] = max(0, min(r - i, p[l + r - i]));
			while (s[i - p[i]] == s[i + p[i]]) ++p[i];
			if (i + p[i] > r) r = i + p[i], l = i - p[i];
		}
		return vector(1 + ALL(p));
	};
	auto manacher = [&] (string s) {
		string t;
		for (char c: s) t += string("#") + c;
		auto res = manacher_odd(t + "#");
		for (int &x: res) x >>= 1;
		return vector(1 + ALL(res) - 1);
	};
	auto pl = manacher(s);
	s = s + '#';
	int n = s.size();
	vector <int> pos(n), cnt(n), lcp(n);
	iota(ALL(pos), 0);
	sort(ALL(pos), [&] (int a, int b) { return s[a] < s[b]; });
	cnt[pos[0]] = 0;
	FOR(i, 1, n) cnt[pos[i]] = cnt[pos[i - 1]] + (s[pos[i]] != s[pos[i - 1]]);
	auto countSort = [&] (vector <int> &p, vector <int> &c) {
		vector <int> pos(n), new_p(n);
		for (int &x: c) if (x + 1 < n) ++pos[x + 1];
		partial_sum(ALL(pos), pos.begin());
		for (int &x: p) new_p[pos[c[x]]++] = x;
		p = move(new_p);
	};
	for (int k = 1; k < n; k <<= 1) {
		REP(i, n) {
			pos[i] = pos[i] - k;
			if (pos[i] < 0) pos[i] += n;
		}
		countSort(pos, cnt);
		vector <int> new_c(n);
		new_c[pos[0]] = 0;
		pair <int, int> prv(cnt[pos[0]], cnt[pos[0] + k - (pos[0] + k < n ? 0 : n)]);
		FOR(i, 1, n) {
			pair <int, int> cur(cnt[pos[i]], cnt[pos[i] + k - (pos[i] + k < n ? 0 : n)]);
			new_c[pos[i]] = new_c[pos[i - 1]] + (prv != cur);
			prv = cur;
		}
		cnt = move(new_c);
	}
	for (int i = 0, k = 0; i < n - 1; ++i) {
		int j = pos[cnt[i] - 1];
		while (s[i + k] == s[j + k]) ++k;
		lcp[cnt[i]] = k;
		k = max(k - 1, 0);
	}
	vector <vector <int>> add_odd(n), add_even(n), del_odd(n), del_even(n);
	REP(i, pl.size()) if (pl[i]) {
		int p = i / 2;
		if (i & 1) {
			add_even[p - pl[i] + 1].push_back(p);
			del_even[p + pl[i]].push_back(p);
		} else {
			add_odd[p - pl[i] + 1].push_back(p);
			del_odd[p + pl[i] - 1].push_back(p);
		}
	}
	set <int> ms_odd, ms_even;
	vector <int> max_palin(n, 0);
	REP(i, n - 1) {
		for (int x: add_odd[i]) ms_odd.insert(x);
		for (int x: add_even[i]) ms_even.insert(x);
		int upper = lcp[cnt[i]];
		if (upper + 2 * i - 1 >= 0) {
			auto it = ms_odd.upper_bound((upper + 2 * i - 1) / 2);
			if (it != ms_odd.begin()) maximize(max_palin[i], (*prev(it) - i) * 2 + 1);
		}
		if (upper + 2 * i - 2 >= 0) {
			auto it = ms_even.upper_bound((upper + 2 * i - 2) / 2);
			if (it != ms_even.begin()) maximize(max_palin[i], (*prev(it) - i + 1) * 2);
		}
		for (int x: del_odd[i]) ms_odd.erase(x);
		for (int x: del_even[i]) ms_even.erase(x);
	}
	stack <int> st;
	vector <int> left(n);
	REP(i, n) {
		while (!st.empty() && lcp[st.top()] >= lcp[i]) st.pop();
		left[i] = st.empty() ? 0 : st.top();
		st.push(i);
	}
	while (!st.empty()) st.pop();
	long long res = 0;
	REP(i, pl.size()) maximize(res, pl[i] * 2 - !(i & 1));
	REPD(i, n) {
		while (!st.empty() && lcp[st.top()] >= lcp[i]) st.pop();
		maximize(res, 1LL * ((st.empty() ? n : st.top()) - left[i]) * max_palin[pos[i]]);
		st.push(i);
	}
	cout << res << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("palindrome");
	// int t; cin >> t; while (t--)
	process();
	// cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:156:2: note: in expansion of macro 'file'
  156 |  file("palindrome");
      |  ^~~~
palindrome.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:156:2: note: in expansion of macro 'file'
  156 |  file("palindrome");
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2108 KB Output is correct
2 Correct 4 ms 2124 KB Output is correct
3 Correct 7 ms 2636 KB Output is correct
4 Correct 6 ms 2636 KB Output is correct
5 Correct 4 ms 1868 KB Output is correct
6 Correct 4 ms 2112 KB Output is correct
7 Correct 4 ms 2112 KB Output is correct
8 Correct 4 ms 2112 KB Output is correct
9 Correct 4 ms 2112 KB Output is correct
10 Correct 4 ms 2368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 18232 KB Output is correct
2 Correct 54 ms 17328 KB Output is correct
3 Correct 86 ms 24000 KB Output is correct
4 Correct 77 ms 22404 KB Output is correct
5 Correct 44 ms 15896 KB Output is correct
6 Correct 52 ms 16044 KB Output is correct
7 Correct 54 ms 18372 KB Output is correct
8 Correct 46 ms 18880 KB Output is correct
9 Correct 51 ms 19916 KB Output is correct
10 Correct 55 ms 19360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 188 ms 53896 KB Output is correct
2 Correct 147 ms 48812 KB Output is correct
3 Correct 292 ms 71520 KB Output is correct
4 Correct 239 ms 62528 KB Output is correct
5 Correct 146 ms 47172 KB Output is correct
6 Correct 163 ms 52908 KB Output is correct
7 Correct 173 ms 53092 KB Output is correct
8 Correct 152 ms 56108 KB Output is correct
9 Correct 154 ms 56092 KB Output is correct
10 Correct 200 ms 57764 KB Output is correct
11 Correct 177 ms 54440 KB Output is correct
12 Correct 158 ms 57008 KB Output is correct