답안 #857020

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
857020 2023-10-05T08:57:00 Z hqminhuwu Palinilap (COI16_palinilap) C++17
0 / 100
42 ms 35408 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];
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";
        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][s[i + l] - 'a'] += nl - l;
            res[i + l][s[i - l] - 'a'] += nl - l;
        }
    }     
    forr (i,1,n - 1){
        int 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 - nl][s[i + nl + 1] - 'a'] += r - l;
            res[i + nl + 1][s[i - nl] - 'a'] += r - l;
            //cout << i << " " << l << " " << r << "\n";
        }
    }   
    forr (i,1,n){
        preadd[i] += preadd[i - 1];
        pre[i] += preadd[i] + pre[i - 1];
    }
    ford (i,n,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);
    }
    cout << ans << "\n";

    return 0;
}
/*



*/

# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 10840 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 42 ms 35408 KB Output isn't correct
2 Halted 0 ms 0 KB -