답안 #857106

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
857106 2023-10-05T11:35:12 Z hqminhuwu Palinilap (COI16_palinilap) C++17
100 / 100
45 ms 39260 KB
#include <bits/stdc++.h>
#define forr(_a,_b,_c) for(_a = _b; _a <= _c; ++_a)
#define ford(_a,_b,_c) for(_a = (_b) + 1; _a --> _c;)
#define forf(_a,_b,_c) for(_a = _b; _a < _c; ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define file "test"


using namespace std;
const int N = 5e5 + 5;
const ll oo = 1e9;
const ll mod = 1e9 + 7;
const ll base = 33577;

ll h[2][N],pw[N],f[N];
ll get (int i, int l, int r){
    return (h[i][r] - h[i][l - 1] * pw[r - l + 1] + mod * mod) % mod;
}
int i;
string s;
ll preadd[N],sufadd[N],res[N][26],pre[N],suf[N];
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> s;
    int n = s.size();
    pw[0] = 1;
    string rs = s;
    reverse(all(rs));
    forr (i,1,n)
        pw[i] = (pw[i - 1] * base) % mod,
        h[0][i] = (h[0][i - 1] * base + s[i - 1]) % mod,
        h[1][i] = (h[1][i - 1] * base + rs[i - 1]) % mod;
    forr (i,1,n){
        int l = 0, r = min (n - i,i - 1);
        while (l < r){
            int mid = (l + r + 1)/2;
            if (get(0,i - mid,i) == get(1,n - i + 1 - mid,n - i + 1))
                l = mid;
            else r = mid - 1;
        }
        //cout << l << "\n";
        f[i] = l;
        preadd[i]++; 
        preadd[i + l + 1]--;
        sufadd[i]++;
        sufadd[i - l - 1]--;
        if (i + l < n && i - l > 1){
            int nl = l + 1, r = min(n - i,i - 1);
            while (nl < r){
                int mid = (nl + r + 1)/2;
                //cout << i - mid << " " << i - l - 2 << " " << n - i + 1 - mid << " " << n - i - l - 1 << "\n";
               // cout << get(0,i - mid,i - l - 2) << " " << get(1,n - i - mid + 1, n - i - l - 1) << "\n";
                if (get(0,i - mid,i - l - 2) == get(1,n - i - mid + 1, n - i - l - 1))
                    nl = mid;
                else r = mid - 1; 
            }
            res[i - l - 1][s[i + l] - 'a'] += nl - l ;
            res[i + l + 1][s[i - l - 2] - 'a'] += nl - l ;
            //cout << i << " " << l << " " << nl << "\n";
            //cout << i << " " << i - l - 1 << " " << i + l + 1 << " " << nl - l << "\n";
        }
        l = 0, r = min (n - i - 1,i - 1);
        while (l < r){
            int mid = (l + r + 1)/2;
            //cout << i - mid << " " << i << " " << n - i - mid << " " << n - i << "\n";
            if (get(0,i - mid,i) == get(1,n - i - mid,n - i))
                l = mid;
            else r = mid - 1;
        }
        if (s[i] != s[i - 1])
            l = -1;
        //cout << l << "\n";
        if (l != -1){
            preadd[i + 1]++; 
            preadd[i + l + 2]--;
            sufadd[i]++;
            sufadd[i - l - 1]--;
        }
        if (i + l < n && i - l > 1){
            int nl = l + 1, r = min(n - i - 1,i - 1);
            //cout << nl << " " << r << "\n";
            while (nl < r){
                int mid = (nl + r + 1)/2;
                 //cout << i - mid << " " << i - l - 2 << " " << n - i - mid << " " << n - i - l - 2 << "\n";
                 //cout << get(0,i - mid,i - l - 2) << " " << get(1,n - i - mid, n - i - l - 2) << "\n";
                if (get(0,i - mid,i - l - 2) == get(1,n - i - mid, n - i - l - 2))
                    nl = mid;
                else r = mid - 1; 
            }
            res[i - l - 1][s[i + l + 1] - 'a'] += nl - l;
            res[i + l + 2][s[i - l - 2] - 'a'] += nl - l;
            //cout << i << " " << i - l - 1 << " " << i + l + 2 << " " << nl - l << "\n";
        }
    }   
    forr (i,0,n){
        preadd[i] += preadd[i - 1];
        pre[i] += preadd[i] + pre[i - 1];
    }
    ford (i,n + 1,1){
        sufadd[i] += sufadd[i + 1];
        suf[i] = sufadd[i] + suf[i + 1];
    }
    ll ans = 0,j;
    forr (i,1,n){
        ans = max (ans,max(suf[i + 1] + pre[i],pre[i - 1] + suf[i]));
        ll mx = 0;
        forr (j,0,25)
            mx = max (mx,res[i][j]);
        ans = max (ans,pre[i - 1] + suf[i + 1] + mx + 1 + f[i]);
        //cout << mx << " ";
    }
    cout << ans << "\n";

    return 0;
}
/*



*/

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10588 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 3 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 39248 KB Output is correct
2 Correct 25 ms 39248 KB Output is correct
3 Correct 27 ms 39260 KB Output is correct
4 Correct 40 ms 39260 KB Output is correct
5 Correct 39 ms 39224 KB Output is correct
6 Correct 39 ms 39120 KB Output is correct
7 Correct 40 ms 39248 KB Output is correct
8 Correct 27 ms 39100 KB Output is correct
9 Correct 39 ms 39136 KB Output is correct
10 Correct 42 ms 39112 KB Output is correct
11 Correct 28 ms 39252 KB Output is correct
12 Correct 45 ms 39252 KB Output is correct
13 Correct 42 ms 39256 KB Output is correct
14 Correct 40 ms 39252 KB Output is correct
15 Correct 40 ms 39252 KB Output is correct
16 Correct 33 ms 39252 KB Output is correct
17 Correct 37 ms 39252 KB Output is correct
18 Correct 39 ms 39120 KB Output is correct
19 Correct 38 ms 39124 KB Output is correct