Submission #40075

# Submission time Handle Problem Language Result Execution time Memory
40075 2018-01-26T12:44:02 Z krauch Palinilap (COI16_palinilap) C++14
0 / 100
74 ms 97844 KB
/*
 _    _    _______   _    _
| |  / /  |  _____| | |  / /
| | / /   | |       | | / /
| |/ /    | |_____  | |/ /
| |\ \    |  _____| | |\ \
| | \ \   | |       | | \ \
| |  \ \  | |_____  | |  \ \
|_|   \_\ |_______| |_|   \_\

*/
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair < ll, int > PLI;


#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define right(x) x << 1 | 1
#define left(x) x << 1
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define mkp make_pair
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define y1 kekekek

#define fname ""

const ll ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const ld eps = 1e-7;
const int N = 2e5 + 6;

string t;
ll pref[N], suff[N], cnt1[N], cnt2[N], val1[N], val2[N], d1[N], d2[N], p[N], pr = 31, addl[N][26], addr[N][26];
char s[N];

ll get1(int l, int r) {
    return (pref[r] - pref[l - 1] * p[r - l + 1] % base + base) % base;
}

ll get2(int l, int r) {
    return (suff[l] - suff[r + 1] * p[r - l + 1] % base + base) % base;
}

int main() {
	ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

	#ifdef krauch
        freopen("input.txt", "r", stdin);
    #else
        //freopen(fname".in", "r", stdin);
        //freopen(fname".out", "w", stdout);
    #endif

    cin >> t;
    int n = sz(t);
    forn(i, 0, n - 1) s[i + 1] = t[i];

    p[0] = 1;
    forn(i, 1, n) {
        pref[i] = (pref[i - 1] * pr % base + s[i] - 'a') % base;
        p[i] = p[i - 1] * pr % base;
    }
    for1(i, n, 1) {
        suff[i] = (suff[i + 1] * pr % base + s[i] - 'a') % base;
    }

    int l = 0, r = -1;
    forn(i, 1, n) {
        if (i <= r) d1[i] = min(d1[l + r - i], 1ll * r - i + 1);
        while (i + d1[i] <= n && i - d1[i] > 0 && s[i + d1[i]] == s[i - d1[i]]) ++d1[i];
        if (i + d1[i] - 1 > r) {
            l = i - d1[i] + 1;
            r = i + d1[i] - 1;
        }

        if (i + d1[i] <= n && i - d1[i] > 0) {
            int tl = i + d1[i] + 1, tr = min(1ll * n, i + d1[i] + (i - d1[i] - 1));
            while (tl < tr) {
                int mid = (tl + tr) >> 1;
                if (get1(i + d1[i] + 1, mid) == get2(i - d1[i] - 1 - (mid - (i + d1[i] + 1)), i - d1[i] - 1)) tl = mid + 1;
                else tr = mid;
            }
            if (tl <= n && get1(i + d1[i] + 1, tl) == get2(i - d1[i] - 1 - (tl - (i + d1[i] + 1)), i - d1[i] - 1)) ++tl;
            addr[i + d1[i]][s[i - d1[i]] - 'a'] += tl - (i + d1[i]);
            addl[i - d1[i]][s[i + d1[i]] - 'a'] += tl - (i + d1[i]);
        }

        cnt1[i - d1[i] + 1]++;
        val1[i - d1[i] + 1]++;
        val1[i] -= d1[i];
        cnt1[i]--;
        cnt2[i + 1]++;
        val2[i + 1] += d1[i] - 1;
        cnt2[i + d1[i]]--;
    }

    l = 0, r = -1;
    forn(i, 1, n) {
        if (i <= r) d2[i] = min(d2[l + r - i + 1], 1ll * r - i + 1) + 1;
        while (i + d2[i] - 1 <= n && i - d2[i] > 0 && s[i + d2[i] - 1] == s[i - d2[i]]) ++d2[i];
        --d2[i];
        if (i + d2[i] - 1 > r) {
            l = i - d2[i];
            r = i + d2[i] - 1;
        }

        if (i + d2[i] <= n && i - d2[i] - 1 > 0) {
            int tl = i + d2[i] + 1, tr = min(1ll * n, i + d2[i] + (i - d2[i] - 2));
            while (tl < tr) {
                int mid = (tl + tr) >> 1;
                if (get1(i + d2[i] + 1, mid) == get2(i - d2[i] - 2 - (mid - (i + d2[i] + 1)), i - d2[i] - 2)) tl = mid + 1;
                else tr = mid;
            }
            if (tl <= n && get1(i + d2[i] + 1, tl) == get2(i - d2[i] - 2 - (tl - (i + d2[i] + 1)), i - d2[i] - 2)) ++tl;
            addr[i + d2[i]][s[i - d2[i] - 1] - 'a'] += tl - (i + d2[i]);
            addl[i - d2[i] - 1][s[i + d2[i]] - 'a'] += tl - (i + d2[i]);
        }

        cnt1[i - d2[i]]++;
        val1[i - d2[i]]++;
        val1[i] -= d2[i] + 1;
        cnt1[i]--;
        cnt2[i]++;
        val2[i] += d2[i];
        cnt2[i + d2[i]]--;
    }

    ll sum = 0;
    forn(i, 1, n) sum += d1[i] + d2[i];

    ll ans = sum;
    ll res1 = 0, x1 = 0, res2 = 0, x2 = 0;
    forn(i, 1, n) {
        res1 += x1;
        res2 -= x2;
        x1 += cnt1[i];
        x2 += cnt2[i];
        res1 += val1[i];
        res2 += val2[i];

        forn(j, 0, 25) {
            ans = max(ans, sum - res1 - res2 + addl[i][j] + addr[i][j]);
        }
    }

    cout << ans << "\n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 97692 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 97692 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 74 ms 97844 KB Output isn't correct
2 Halted 0 ms 0 KB -