Submission #647896

#TimeUsernameProblemLanguageResultExecution timeMemory
647896ghostwriter회문 (APIO14_palindrome)C++14
47 / 100
608 ms26172 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int M = 1000000207;
const int base = 29;
const int N = 3e5 + 5;
int n, d[N];
str s;
int p[N], h[N], h1[N];
vpi v;
vi v1, v2;
map<int, bool> d1;
ll rs = 0;
int geth(const int &l, const int &r) { return (h[r] - 1LL * h[l - 1] * p[r - l + 1] % M + M) % M; }
int geth1(const int &l, const int &r) { return (h1[l] - 1LL * h1[r + 1] * p[r - l + 1] % M + M) % M; }
bool cmp(const pi &a, const pi &b) { return a.nd - a.st + 1 < b.nd - b.st + 1; }
int getpos(const int &x) { if (x > v1.bk()) return -1; int ans = lb(all(v1), x) - v1.bg(); return x == v1[ans]? ans : -1; }
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    p[0] = 1;
    FOR(i, 1, N - 1) p[i] = 1LL * p[i - 1] * base % M;
    cin >> s;
    n = sz(s);
    s = "#" + s + "#";
    FOR(i, 1, n) h[i] = (1LL * h[i - 1] * base % M + s[i] - 'a' + 1) % M;
    FOS(i, n, 1) h1[i] = (1LL * h1[i + 1] * base % M + s[i] - 'a' + 1) % M;
    FOR(i, 1, n) {
    	int l = 1, r = min(i - 1, n - i), ans = 0;
    	WHILE(l <= r) {
    		int mid = l + (r - l) / 2;
    		if (geth(i - mid, i + mid) == geth1(i - mid, i + mid)) {
    			ans = mid;
    			l = mid + 1;
    		}
    		else r = mid - 1;
    	}
        l = i - ans;
        r = i + ans;
        int curh = geth(l, r);
        v2.pb(curh);
        WHILE(!d1[curh]) {
            v.pb({l, r});
            d1[curh] = 1;
            ++l;
            --r;
            if (l > r) break;
            curh = geth(l, r);
        }
    }
    FOR(i, 1, n - 1) {
    	if (s[i] != s[i + 1]) continue;
    	int l = 1, r = min(i - 1, n - i - 1), ans = 0;
    	WHILE(l <= r) {
    		int mid = l + (r - l) / 2;
    		if (geth(i - mid, i + 1 + mid) == geth1(i - mid, i + 1 + mid)) {
    			ans = mid;
    			l = mid + 1;
    		}
    		else r = mid - 1;
    	}
        l = i - ans;
        r = i + 1 + ans;
        int curh = geth(l, r);
        v2.pb(curh);
        WHILE(!d1[curh]) {
            v.pb({l, r});
            d1[curh] = 1;
            ++l;
            --r;
            if (l > r) break;
            curh = geth(l, r);
        }
    }
    sort(all(v), cmp);
    EACH(i, v) v1.pb(geth(i.st, i.nd));
    sort(all(v1));
    EACH(i, v2) ++d[getpos(i)];
    FSN(i, sz(v)) {
        int ln = v[i].st, rn = v[i].nd, len = rn - ln + 1;
        int posh = getpos(geth(ln, rn));
        rs = max(rs, 1LL * d[posh] * len);
        if (ln + 1 <= rn - 1) d[getpos(geth(ln + 1, rn - 1))] += d[posh];
    }
    cout << rs;
    return 0;
}
/*
abbabaaa
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

palindrome.cpp: In function 'int main()':
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:70:5: note: in expansion of macro 'FOR'
   70 |     FOR(i, 1, N - 1) p[i] = 1LL * p[i - 1] * base % M;
      |     ^~~
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:74:5: note: in expansion of macro 'FOR'
   74 |     FOR(i, 1, n) h[i] = (1LL * h[i - 1] * base % M + s[i] - 'a' + 1) % M;
      |     ^~~
palindrome.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
palindrome.cpp:75:5: note: in expansion of macro 'FOS'
   75 |     FOS(i, n, 1) h1[i] = (1LL * h1[i + 1] * base % M + s[i] - 'a' + 1) % M;
      |     ^~~
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:76:5: note: in expansion of macro 'FOR'
   76 |     FOR(i, 1, n) {
      |     ^~~
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:99:5: note: in expansion of macro 'FOR'
   99 |     FOR(i, 1, n - 1) {
      |     ^~~
palindrome.cpp:36:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
palindrome.cpp:124:5: note: in expansion of macro 'EACH'
  124 |     EACH(i, v) v1.pb(geth(i.st, i.nd));
      |     ^~~~
palindrome.cpp:36:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
palindrome.cpp:126:5: note: in expansion of macro 'EACH'
  126 |     EACH(i, v2) ++d[getpos(i)];
      |     ^~~~
palindrome.cpp:35:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   35 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
palindrome.cpp:127:5: note: in expansion of macro 'FSN'
  127 |     FSN(i, sz(v)) {
      |     ^~~
#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...