Submission #42184

#TimeUsernameProblemLanguageResultExecution timeMemory
42184cmasterPalindromes (APIO14_palindrome)C++14
23 / 100
1066 ms16100 KiB
#include <bits/stdc++.h> /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp>*/ #define pb push_back #define mp make_pair #define sz(s) ((int)(s.size())) #define all(s) s.begin(), s.end() #define rep(i, a, n) for (int i = a; i <= n; ++i) #define per(i, n, a) for (int i = n; i >= a; --i) #define onlycin ios_base::sync_with_stdio(false); cin.tie(0) using namespace std; // using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; /*typedef tree< pair < int, int >, null_type, less< pair < int, int > >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;*/ // find_by_order() order_of_key() const int MAXN = (int)1e5+228; const char nxtl = '\n'; const int mod = (int)1e9+7; const double eps = (double)1e-7; template<typename T> inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;} template<typename T> inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;} int n, suff[MAXN], c[MAXN], nc[MAXN], tmp[MAXN], cnt[MAXN], timer; char s[MAXN]; int lcp[MAXN], pos[MAXN]; inline void calc() { rep(i, 1, max(n, 256)) cnt[i] += cnt[i - 1]; per(i, max(n, 256), 0) cnt[i + 1] = cnt[i]; cnt[0] = 0; } inline void SA() { memset(cnt, 0, sizeof cnt); s[n++] = (char)0; rep(i, 0, n - 1) cnt[s[i]]++; calc(); rep(i, 0, n - 1) { suff[cnt[s[i]]++] = i; } c[suff[0]] = 0; rep(i, 1, n - 1) { if(s[suff[i]] == s[suff[i - 1]]) c[suff[i]] = c[suff[i - 1]]; else c[suff[i]] = c[suff[i - 1]] + 1; } for(int len = 1; len <= n; len += len) { rep(i, 0, n - 1) tmp[i] = (suff[i] - len + n) % n; memset(cnt, 0, sizeof cnt); rep(i, 0, n - 1) cnt[c[suff[i]]]++; calc(); rep(i, 0, n - 1) suff[cnt[c[tmp[i]]]++] = tmp[i]; nc[suff[0]] = 0; rep(i, 1, n - 1) { pair < int, int > cur = { c[suff[i]], c[(suff[i] + len) % n] }; pair < int, int > last = { c[suff[i - 1]], c[(suff[i - 1] + len) % n] }; if(cur == last) nc[suff[i]] = nc[suff[i - 1]]; else nc[suff[i]] = nc[suff[i - 1]] + 1; } rep(i, 0, n - 1) c[i] = nc[i]; } n--; rep(i, 0, n - 1) suff[i] = suff[i + 1]; } void build() { rep(i, 0, n - 1) pos[suff[i]] = i; int k = 0; rep(i, 0, n - 1) { if(k > 0) k--; if(pos[i] == n - 1) { k = 0; lcp[n - 1] = 0; } else { int j = suff[pos[i] + 1]; while(k <= n && max(i + k, j + k) < n && s[(i + k) % n] == s[(j + k) % n]) k++; lcp[pos[i]] = k; } } } bool ok(int l, int r) { if(l >= r) return 1; if(s[l] != s[r]) return 0; return ok(l+1, r-1); } int sv[MAXN]; ll get(int i, int len1) { int l = i, kol = 1, r = i+1; while(l >= 0 && lcp[l] >= len1) l--; while(r <= n-2 && lcp[r] >= len1) r++; l++, r--; kol += r - l + 1; return kol*1ll*len1; } vector < int > have[MAXN]; int main() { #ifdef accepted freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif // onlycin; scanf("%s", s); n = strlen(s); SA(); build(); ll res = 0ll; int mx = 0; rep(i, 0, n-1) { int len = n-suff[i]; rep(j, 1, len) { if(ok(suff[i], suff[i]+j-1)) have[i].pb(j); } } rep(i, 0, n-1) { int L = i, R = i; while(L >= 0 && lcp[L] >= lcp[i]) L--; while(R <= n-2 && lcp[R] >= lcp[i]) R++; L++, R--; /* cerr << i << " -->\n"; for(auto &to : have[i]) cerr << to << ' '; cerr << nxtl;*/ if(!have[i].empty()) { updmax(res, have[i].back()*1ll); } if(i < n-1) { auto it = upper_bound(all(have[i]), lcp[i])-have[i].begin(); if(it > 0) updmax(res, have[i][it-1]*1ll*(R-L+2)); } } cout << res << nxtl; return 0; }

Compilation message (stderr)

palindrome.cpp: In function 'void SA()':
palindrome.cpp:45:27: warning: array subscript has type 'char' [-Wchar-subscripts]
  rep(i, 0, n - 1) cnt[s[i]]++;
                           ^
palindrome.cpp:48:16: warning: array subscript has type 'char' [-Wchar-subscripts]
   suff[cnt[s[i]]++] = i;
                ^
palindrome.cpp: In function 'int main()':
palindrome.cpp:118:6: warning: unused variable 'mx' [-Wunused-variable]
  int mx = 0;
      ^
palindrome.cpp:113:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", s);
                ^
#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...