Submission #539036

#TimeUsernameProblemLanguageResultExecution timeMemory
539036cpp219Palinilap (COI16_palinilap)C++17
0 / 100
45 ms24936 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; 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){ //debug(pw[1]); //debug(pw[r - l + 1]); return (Hash[r][c] - Hash[l - 1][c]*pw[r - l + 1] + mod*mod)%mod; } bool IsPalin(ll l,ll r){ //debug(GetHash(l,r,0)); //debug(Hash[3][1]); //debug(GetHash(n - r + 1,n - l + 1,1)); 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 "tst" 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 = 1,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; //cout<<i<<" "<<j<<" "<<rad<<"\n"; have[0][i]++; have[0][i - rad]--; have[1][j]++; have[1][j + rad]--; if (i - rad <= 0 || j + rad > n) continue; l = 0; h = min(i - rad,n - j - rad + 1); 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; } creat[x][s[0][y] - 'a'] += h + 1; creat[y][s[0][x] - 'a'] += h + 1; } //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; //debug(creat[2][2]); 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); //cout<<i<<" "<<kq + mx<<"\n"; } cout<<ans; } /// be confident

Compilation message (stderr)

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