Submission #676321

#TimeUsernameProblemLanguageResultExecution timeMemory
676321vuavisaoPalindromes (APIO14_palindrome)C++14
23 / 100
1085 ms8356 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ll long long using namespace std; template<typename Lhs, typename Rhs> inline bool Max_self(Lhs &a, Rhs b) { if(b > a) { a = b; return true; } return false; } template<typename Lhs, typename Rhs> inline bool Min_self(Lhs &a, Rhs b) { if(b < a) { a = b; return true; } return false; } const int N = 3e5 + 10; int n; string s; namespace sub123 { bool check() { return n <= 10000; } const uint8_t size_T = 2; const int _1hrd = 100; const int _1bil = 1000000000; const array<int, size_T> MOD = {_1bil + 9, _1bil + 11}; const array<int, size_T> BASE = {_1hrd + 28, _1hrd + 31}; struct Hash { array<int, size_T> val = {0}; Hash() {}; Hash (int x) { for (uint8_t i = 0; i < size_T; ++ i) this -> val[i] = x % MOD[i]; } bool operator < (const Hash& other) const { for (uint8_t i = 0; i < size_T; ++ i) if (this -> val[i] != other.val[i]) return this -> val[i] < other.val[i]; return false; } bool operator == (const Hash& other) const { for (uint8_t i = 0; i < size_T; ++ i) if (this -> val[i] != other.val[i]) return false; return true; } Hash operator + (const int& x) const { Hash res; for (uint8_t i = 0; i < size_T; ++ i) res.val[i] = (1ll * this -> val[i] * BASE[i] + x) % MOD[i]; return res; } Hash operator + (const Hash& other) const { Hash res; for (uint8_t i = 0; i < size_T; ++ i) res.val[i] = (this -> val[i] + other.val[i]) % MOD[i]; return res; } Hash operator - (const Hash& other) const { Hash res; for (uint8_t i = 0; i < size_T; ++ i) { res.val[i] = (this -> val[i] - other.val[i]) % MOD[i]; res.val[i] = (res.val[i] + MOD[i]) % MOD[i]; } return res; } Hash operator * (const Hash& other) const { Hash res; for (uint8_t i = 0; i < size_T; ++ i) res.val[i] = (1ll * this -> val[i] * other.val[i]) % MOD[i]; return res; } friend ostream& operator << (ostream& os, Hash& other) { os << "[ "; for (uint8_t i = 0; i < size_T; ++ i) { cout << other.val[i]; if(i < size_T - 1) cout << ','; cout << ' '; } os << "]"; return os; } }; Hash p[N], h[N], h_rev[N]; int res; Hash get_hash(int l, int r, Hash* h) { return h[r] - h[l - 1] * p[r - l + 1]; } void solve() { p[0] = 1; for(int i = 1; i <= n; ++ i) { p[i] = p[i - 1] + 0; h[i] = h[i - 1] + s[i]; h_rev[i] = h_rev[i - 1] + s[n - i + 1]; } for(int len = 1; len <= n; ++ len) { vector<Hash> candidates = {}; auto palin = [&] (int l, int r) -> bool { return get_hash(l, r, h) == get_hash(n - r + 1, n - l + 1, h_rev); }; for(int i = 1; i <= n - len + 1; ++ i) { if(palin(i, i + len - 1)) candidates.push_back(get_hash(i, i + len - 1, h)); } sort(candidates.begin(), candidates.end()); for(int i = 0, j = 0; i < (int) candidates.size(); i = j) { while(candidates[j] == candidates[i]) ++ j; Max_self(res, (j - i) * len); } } cout << res; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("APIO14_palindrome.inp", "r")) { freopen("APIO14_palindrome.inp", "r", stdin); freopen("APIO14_palindrome.out", "w", stdout); } cin >> s; n = (int) s.size(); s = ' ' + s; if(sub123::check()) { sub123::solve(); return 0; } return 0; } /// Code by vuavisao

Compilation message (stderr)

palindrome.cpp: In function 'int32_t main()':
palindrome.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen("APIO14_palindrome.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen("APIO14_palindrome.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...