제출 #539065

#제출 시각아이디문제언어결과실행 시간메모리
539065cpp219Palinilap (COI16_palinilap)C++17
100 / 100
66 ms25908 KiB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e5 + 9;
const ll base = 31;
const ll mod = 1e9 + 7;

ll n,Hash[N][2],pw[N],have[2][N],creat[N][26],ans,got[N];
string s[2];

void performHash(){
    for (ll i = 1;i <= n;i++)
        for (ll j = 0;j < 2;j++){
            Hash[i][j] = Hash[i - 1][j]*base + s[j][i] - 'a' + 1;
            Hash[i][j] %= mod;
        }
}

ll GetHash(ll l,ll r,ll c){
    return (Hash[r][c] - Hash[l - 1][c]*pw[r - l + 1] + mod*mod)%mod;
}

bool IsPalin(ll l,ll r){
    return GetHash(l,r,0) == GetHash(n - r + 1,n - l + 1,1);
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin>>s[0]; n = s[0].size();
    s[1] = s[0]; reverse(s[1].begin(),s[1].end());
    s[0] = " " + s[0]; s[1] = " " + s[1];
    performHash(); pw[0] = 1;
    for (ll i = 1;i <= n;i++) pw[i] = (pw[i - 1]*base) % mod;
    //debug(IsPalin(1,3));
    for (ll i = 1;i <= n;i++)
        for (ll j = i;j <= i + 1;j++){
            //if (j > n || s[0][i] != s[0][j]) continue;
            ll l = (s[0][i] == s[0][j]),h = min(i,n - j + 1),m;
            while(l <= h){
                m = (l + h)/2;
                if (IsPalin(i - m + 1,j + m - 1)) l = m + 1;
                else h = m - 1;
            }
            ll rad = h; ans += rad; //debug(rad);
            have[0][i]++; have[0][i - rad]--;
            have[1][j]++; have[1][j + rad]--;
            if (i == j) got[i] += rad - 1;
            if (i - rad <= 0 || j + rad > n) continue;
            l = 0; h = min(i - rad - 1,n - j - rad);
            ll x = i - rad,y = j + rad;
            while(l <= h){
                m = (l + h)/2;
                ll kq1 = GetHash(x - m,x - 1,0);
                ll kq2 = GetHash(n - y - m + 1,n - y,1);
                if (kq1 == kq2) l = m + 1;
                else h = m - 1;
            }
            //debug(h);
            //cout<<x<<" "<<y; return 0;
            creat[x][s[0][y] - 'a'] += h + 1;
            creat[y][s[0][x] - 'a'] += h + 1;
        }
    //debug(creat[2]['c' - 'a']);
    //for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; cout<<"\n";
    for (ll i = 2;i <= n;i++) have[1][i] += have[1][i - 1];
    //for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; return 0;
    for (ll i = 2;i <= n;i++) have[1][i] += have[1][i - 1];

    for (ll i = n - 1;i > 0;i--) have[0][i] += have[0][i + 1];

    for (ll i = n - 1;i > 0;i--) have[0][i] += have[0][i + 1];
    //for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; return 0;

    for (ll i = 1;i <= n;i++){
        ll kq = have[0][i + 1] + have[1][i - 1],mx = 0;
        for (ll j = 0;j < 26;j++) mx = max(mx,creat[i][j]);
        ans = max(ans,kq + mx + 1 + got[i]); //cout<<i<<" "<<kq + mx<<"\n";
    }
    cout<<ans;
}
/// be confident

컴파일 시 표준 에러 (stderr) 메시지

palinilap.cpp: In function 'int main()':
palinilap.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palinilap.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...