제출 #879399

#제출 시각아이디문제언어결과실행 시간메모리
879399marvinthang회문 (APIO14_palindrome)C++17
15 / 100
205 ms53604 KiB
/************************************* * 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 < 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); // cout << add_odd[i] << '\n'; // cout << add_even[i] << '\n'; int upper = lcp[cnt[i]]; // (v - i) * 2 + 1 <= upper // v <= (upper + 2i - 1) / 2 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); // (i - v) * 2 + 1 <= upper // v >= (2i - upper + 1) / 2 it = ms_odd.lower_bound((2 * i - upper + 2) / 2); if (it != ms_odd.end()) maximize(max_palin[i], (i - *it) * 2 + 1); // (v - i + 1) * 2 <= upper // v <= (upper + 2i - 2) / 2 it = ms_even.upper_bound((upper + 2 * i - 2) / 2); if (it != ms_even.begin()) maximize(max_palin[i], (*prev(it) - i + 1) * 2); // (i - v) * 2 <= upper // v >= (2i - upper) / 2 it = ms_even.lower_bound((2 * i - upper + 1) / 2); if (it != ms_even.end()) maximize(max_palin[i], (i - *it) * 2); for (int x: del_odd[i]) ms_odd.erase(x); for (int x: del_even[i]) ms_even.erase(x); // cout << "------------\n"; } // cout << max_palin << '\n'; 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); } // cout << pos << '\n'; // cout << lcp << '\n'; 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(); // cout << i << ' ' << pos[i] << ' ' << max_palin[pos[i]] << ' ' << left[i] << ' ' << (st.empty() ? n : st.top()) << '\n'; 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); }

컴파일 시 표준 에러 (stderr) 메시지

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:171:2: note: in expansion of macro 'file'
  171 |  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:171:2: note: in expansion of macro 'file'
  171 |  file("palindrome");
      |  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...