제출 #564959

#제출 시각아이디문제언어결과실행 시간메모리
564959ac2hu회문 (APIO14_palindrome)C++14
0 / 100
94 ms79580 KiB
#include <bits/stdc++.h> #ifdef DEBUG #include "../templates/debug.h" #else #define deb(x...) #endif using namespace std; typedef unsigned long long ull; // Generate random base in (before, after) open interval: int gen_base(const int before, const int after) { auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 mt_rand(seed); int base = std::uniform_int_distribution<int>(before+1, after)(mt_rand); return base % 2 == 0 ? base-1 : base; } struct PolyHash {/*{{{*/ // -------- Static variables -------- static const int mod = (int)1e9+123; // prime mod of polynomial hashing static std::vector<int> pow1; // powers of base modulo mod static std::vector<ull> pow2; // powers of base modulo 2^64 static int base; // base (point of hashing) // --------- Static functons -------- static inline int diff(int a, int b) { // Diff between `a` and `b` modulo mod (0 <= a < mod, 0 <= b < mod) return (a -= b) < 0 ? a + mod : a; } // -------------- Variables of class ------------- std::vector<int> pref1; // Hash on prefix modulo mod std::vector<ull> pref2; // Hash on prefix modulo 2^64 // Cunstructor from string: PolyHash(const std::string& s) : pref1(s.size()+1u, 0) , pref2(s.size()+1u, 0) { assert(base < mod); const int n = s.size(); // Firstly calculated needed power of base: while ((int)pow1.size() <= n) { pow1.push_back(1LL * pow1.back() * base % mod); pow2.push_back(pow2.back() * base); } for (int i = 0; i < n; ++i) { // Fill arrays with polynomial hashes on prefix assert(base > s[i]); pref1[i+1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod; pref2[i+1] = pref2[i] + s[i] * pow2[i]; } } // Polynomial hash of subsequence [pos, pos+len) // If mxPow != 0, value automatically multiply on base in needed power. Finally base ^ mxPow inline std::pair<int, ull> operator()(const int pos, const int len, const int mxPow = 0) const { int hash1 = pref1[pos+len] - pref1[pos]; ull hash2 = pref2[pos+len] - pref2[pos]; if (hash1 < 0) hash1 += mod; if (mxPow != 0) { hash1 = 1LL * hash1 * pow1[mxPow-(pos+len-1)] % mod; hash2 *= pow2[mxPow-(pos+len-1)]; } return std::make_pair(hash1, hash2); } };/*}}}*/ int PolyHash::base((int)1e9 + 7); vector<int> PolyHash::pow1{1}; vector<ull> PolyHash::pow2{1}; const int C = 26; struct node{ int child[C]; int cnt = 0; int dist = 0; }; node root1[(int)3e5], root2[(int)3e5]; int r1 = 1, r2 = 1; void make_node_r1(int id, int d){ for(int i = 0;i<C;i++){ root1[id].child[i] = 0; } root1[id].cnt = 0; root1[id].dist = d; } void make_node_r2(int id, int d){ for(int i = 0;i<C;i++){ root2[id].child[i] = 0; } root2[id].cnt = 0; root2[id].dist = d; } string s; int ans = 1; void add1(int l,int r){ int val = 1; for(int i = l;i<=r;i++){ int c = s[i] - 'a'; if(!root1[val].child[c]){ root1[val].child[c] = ++r1; make_node_r1(root1[val].child[c], root1[val].dist + 2); } val= root1[val].child[c]; ++root1[val].cnt; ans = max(ans, root1[val].cnt*root1[val].dist); } } void add2(int l,int r){ int val = 1; for(int i = l;i<=r;i++){ int c = s[i] - 'a'; if(!root2[val].child[c]){ root2[val].child[c] = ++r2; make_node_r2(root2[val].child[c], root2[val].dist + 2); } val= root2[val].child[c]; ++root2[val].cnt; ans = max(ans, root2[val].cnt*root2[val].dist); } } signed main(){ make_node_r1(1,-1); make_node_r2(1,0); cin >> s; string r = s; reverse(r.begin(), r.end()); int n = s.size(); PolyHash nor(s); PolyHash rev(r); auto is_pali = [&](int l,int r){ int siz = (r - l + 1); if(siz%2 == 0){ int m = (l + r)/2; // (i,m) == rev(m + 1, r) auto g = nor(l,m - l + 1, n); auto l = rev(n - r - 1, r - m, n); return (g == l); } else{ int m = (l + r)/2; auto g = nor(l,m - l, n); auto l = rev(n - r - 1, r -m, n); return (g == l); } }; vector<int> leq(n,0); vector<int> req(n,0); for(int i = 1;i<n;i++){ if(s[i] == s[i - 1])leq[i] = 1 + leq[i - 1]; } for(int i = n - 2;i>=0;i--){ if(s[i] == s[i + 1])req[i] = 1 + req[i + 1]; } for(int i = 0;i<n;i++){ auto check = [&](int mid)->bool{ if(i - mid < 0)return false; if(i + mid > n - 1)return false; return is_pali(i - mid, i + mid); }; int l = min(leq[i], req[i]) + 1,r = n - 1; if(l <= r && check(l)){ while(l < r){ int mid = (l + r + 1)/2; if(check(mid)) l = mid; else r = mid - 1; } // add1(i,i + l); } if(i > 0 && s[i] == s[i - 1]){ l = min(leq[i - 1], req[i]) + 1,r = n - 1; if(l <= r && i - 1 - l >=0 && i + l < n && is_pali(i - 1 - l,i + l)){ while(l < r){ int mid = (l + r + 1)/2; if(i - 1 - mid >=0 && i + mid < n && is_pali(i - 1 - mid, i + mid)){ l = mid; } else r = mid - 1; } // add2(i,i + l); } } } // vector<vector<int>> temp(26); // for(int i = 0;i<n;i++){ // temp[s[i] - 'a'].push_back(req[i] + 1); // } // for(int j = 0;j<26;j++){ // vector<int> cc(n + 2, 0); // for(auto e : temp[j]){ // cc[e + 1]--; // cc[0]++; // } // for(int i = 1;i<=n + 1;i++)cc[i] += cc[i - 1]; // for(int i = 0;i<=n;i++){ // ans = max(ans, cc[i]*i); // } // } // cout << ans << "\n"; }
#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...